Skip to content

Commit

Permalink
Update coding style rules (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati authored Jul 31, 2024
1 parent ed76ed5 commit 4e7b43c
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 64 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
phpcs:
name: PHP Coding Style
runs-on: ubuntu-latest
steps:
-
name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.3"
tools: php-cs-fixer
coverage: none
-
name: Checkout
uses: actions/checkout@v4
-
name: Run PHP-CS-Fixer
run: php-cs-fixer check --ansi --no-interaction --using-cache=no --diff --show-progress=none
31 changes: 31 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* This document has been generated with
* https://mlocati.github.io/php-cs-fixer-configurator/#version:3.61.0|configurator
* you can change this configuration by importing this file.
*/

$config = new PhpCsFixer\Config();

return $config
->setRiskyAllowed(true)
->setRules([
'@PER-CS' => true,
'@PER-CS:risky' => true,
// Empty body of class, interface, trait, enum or function must be abbreviated as `{}` and placed on the same line as the previous symbol, separated by a single space.
'single_line_empty_body' => false,
// Multi-line arrays, arguments list, parameters list and `match` expressions must have a trailing comma.
'trailing_comma_in_multiline' => ['after_heredoc' => false, 'elements' => ['arrays']],
// Visibility MUST be declared on all properties and methods; `abstract` and `final` MUST be declared before the visibility; `static` MUST be declared after the visibility.
'visibility_required' => ['elements' => ['method','property']],
])
->setFinder(PhpCsFixer\Finder::create()
->in(__DIR__)
// ->exclude([
// 'folder-to-exclude',
// ])
// ->append([
// 'file-to-include',
// ])
)
;
20 changes: 0 additions & 20 deletions .php_cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Gettext.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function commandIsAvailable($command)
if (!in_array('exec', array_map('trim', explode(',', strtolower(@ini_get('disable_functions')))), true)) {
$rc = 1;
$output = array();
@exec($command.' --version 2>&1', $output, $rc);
@exec($command . ' --version 2>&1', $output, $rc);
if ($rc === 0) {
$cache[$command] = true;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ final protected static function getDirectoryStructure($rootDirectory, $exclude3r
if (!isset(self::$cache[__FUNCTION__])) {
self::$cache[__FUNCTION__] = array();
}
$cacheKey = $rootDirectory.'*'.($exclude3rdParty ? '1' : '0');
$cacheKey = $rootDirectory . '*' . ($exclude3rdParty ? '1' : '0');
if (!isset(self::$cache[__FUNCTION__][$cacheKey])) {
self::$cache[__FUNCTION__][$cacheKey] = static::getDirectoryStructureDo('', $rootDirectory, $exclude3rdParty);
}
Expand All @@ -215,7 +215,7 @@ private static function getDirectoryStructureDo($relativePath, $rootDirectory, $
{
$thisRoot = $rootDirectory;
if ($relativePath !== '') {
$thisRoot .= '/'.$relativePath;
$thisRoot .= '/' . $relativePath;
}
$subDirs = array();
$hDir = @opendir($thisRoot);
Expand All @@ -226,7 +226,7 @@ private static function getDirectoryStructureDo($relativePath, $rootDirectory, $
if ($entry[0] === '.') {
continue;
}
$fullPath = $thisRoot.'/'.$entry;
$fullPath = $thisRoot . '/' . $entry;
if (!is_dir($fullPath)) {
continue;
}
Expand Down Expand Up @@ -256,12 +256,12 @@ private static function getDirectoryStructureDo($relativePath, $rootDirectory, $
final public static function getAllParsers()
{
$result = array();
$dir = __DIR__.'/Parser';
$dir = __DIR__ . '/Parser';
if (is_dir($dir) && is_readable($dir)) {
$matches = null;
foreach (scandir($dir) as $item) {
if (($item[0] !== '.') && preg_match('/^(.+)\.php$/i', $item, $matches)) {
$fqClassName = '\\'.__NAMESPACE__.'\\Parser\\'.$matches[1];
$fqClassName = '\\' . __NAMESPACE__ . '\\Parser\\' . $matches[1];
$result[] = new $fqClassName();
}
}
Expand All @@ -273,7 +273,7 @@ final public static function getAllParsers()
final public static function getByHandle($parserHandle)
{
$parser = null;
$fqClassName = '\\'.__NAMESPACE__.'\\Parser\\'.static::camelifyString($parserHandle);
$fqClassName = '\\' . __NAMESPACE__ . '\\Parser\\' . static::camelifyString($parserHandle);
if (class_exists($fqClassName, true)) {
$parser = new $fqClassName();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/BlockTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDi
$prefix = ($relativePath === '') ? '' : "$relativePath/";
$matches = null;
foreach (array_merge(array(''), $this->getDirectoryStructure($rootDirectory, $exclude3rdParty)) as $child) {
$shownChild = ($child === '') ? rtrim($prefix, '/') : ($prefix.$child);
$shownChild = ($child === '') ? rtrim($prefix, '/') : ($prefix . $child);
$fullpath = ($child === '') ? $rootDirectory : "$rootDirectory/$child";
if (preg_match('%(?:^|/)blocks/\w+/(?:templates|composer)/(\w+)$%', $fullpath, $matches)) {
if (!isset($templateHandles[$matches[1]])) {
Expand All @@ -56,7 +56,7 @@ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDi
if (!isset($templateHandles[$matches[1]])) {
$templateHandles[$matches[1]] = array();
}
$templateHandles[$matches[1]][] = $shownChild."/$file";
$templateHandles[$matches[1]][] = $shownChild . "/$file";
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Parser/Cif.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDi
{
$prefix = ($relativePath === '') ? '' : "$relativePath/";
foreach (array_merge(array(''), $this->getDirectoryStructure($rootDirectory, $exclude3rdParty)) as $child) {
$shownDirectory = $prefix.(($child === '') ? '' : "$child/");
$shownDirectory = $prefix . (($child === '') ? '' : "$child/");
$fullDirectoryPath = ($child === '') ? $rootDirectory : "$rootDirectory/$child";
$contents = @scandir($fullDirectoryPath);
if ($contents === false) {
Expand All @@ -46,7 +46,7 @@ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDi
if ($file[0] !== '.') {
$fullFilePath = "$fullDirectoryPath/$file";
if (preg_match('/^(.*)\.xml$/', $file) && is_file($fullFilePath)) {
static::parseXml($translations, $fullFilePath, $shownDirectory.$file);
static::parseXml($translations, $fullFilePath, $shownDirectory . $file);
}
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@ private static function parseXmlNode(\Gettext\Translations $translations, $filen
default:
throw new \Exception("Unknown node class '$nodeClass' in '$filenameRel'");
}
$path = $prePath.'/'.$node->tagName;
$path = $prePath . '/' . $node->tagName;
$childnodesLimit = null;
switch ($path) {
case '/concrete5-cif':
Expand Down Expand Up @@ -439,7 +439,7 @@ private static function parseXmlNode(\Gettext\Translations $translations, $filen
if (strpos($filenameRel, 'packages/') === 0) {
return;
}
throw new \Exception('Unknown tag name '.$path.' in '.$filenameRel."\n\nNode:\n".$node->ownerDocument->saveXML($node));
throw new \Exception('Unknown tag name ' . $path . ' in ' . $filenameRel . "\n\nNode:\n" . $node->ownerDocument->saveXML($node));
}
if ($node->hasChildNodes()) {
foreach ($node->childNodes as $child) {
Expand Down
12 changes: 6 additions & 6 deletions src/Parser/ConfigFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDi
$corePrefix = 'concrete';
if (defined('DIR_BASE') && defined('DIR_BASE_CORE')) {
$dirBase = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', DIR_BASE), '/');
if ($rootDirectory === $dirBase) {
if ($rootDirectory === $dirBase) {
// We are working on the current Concrete instance
$dirBaseCore = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', DIR_BASE_CORE), '/');
if ($dirBaseCore !== "{$dirBase}/concrete" && strpos($dirBaseCore, "{$dirBase}/") === 0) {
Expand Down Expand Up @@ -86,11 +86,11 @@ private function parseFileTypes(\Gettext\Translations $translations, $rootDirect
{
foreach ($directoryAlternatives as $subDir) {
$rel = ($subDir === '') ? 'app.php' : "$subDir/app.php";
$fileAbs = $rootDirectory.'/'.$rel;
$fileAbs = $rootDirectory . '/' . $rel;
if (!is_file($fileAbs)) {
continue;
}
$fileRel = $prefix.$rel;
$fileRel = $prefix . $rel;
$configFile = new \C5TL\Util\ConfigFile($fileAbs);
$config = $configFile->getArray();
if (isset($config['file_types']) && is_array($config['file_types'])) {
Expand All @@ -117,11 +117,11 @@ private function parseTranslatableStrings(\Gettext\Translations $translations, $
list($baseFilename, $subkey) = explode('.', $key, 2);
foreach ($directoryAlternatives as $subDir) {
$rel = ($subDir === '') ? "{$baseFilename}.php" : "{$subDir}/{$baseFilename}.php";
$fileAbs = $rootDirectory.'/'.$rel;
$fileAbs = $rootDirectory . '/' . $rel;
if (!is_file($fileAbs)) {
continue;
}
$fileRel = $prefix.$rel;
$fileRel = $prefix . $rel;
$configFile = new \C5TL\Util\ConfigFile($fileAbs);
$config = $configFile->getArray();
$subkeys = explode('.', $subkey);
Expand Down Expand Up @@ -154,7 +154,7 @@ private function getTranslatableKeys()
'concrete.user.username.allowed_characters.error_string' => '',
);
}

/**
* Add to the $translations the value read from the configuration.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/Dynamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ protected function parseRunningConcrete5Do(\Gettext\Translations $translations,
public function getSubParsers()
{
$result = array();
$dir = __DIR__.'/DynamicItem';
$dir = __DIR__ . '/DynamicItem';
if (is_dir($dir) && is_readable($dir)) {
$matches = null;
foreach (scandir($dir) as $item) {
if (($item[0] !== '.') && preg_match('/^(.+)\.php$/i', $item, $matches) && ($matches[1] !== 'DynamicItem')) {
$fqClassName = '\\'.__NAMESPACE__.'\\DynamicItem\\'.$matches[1];
$fqClassName = '\\' . __NAMESPACE__ . '\\DynamicItem\\' . $matches[1];
$instance = new $fqClassName();
/* @var $instance \C5TL\Parser\DynamicItem\DynamicItem */
$result[$instance->getDynamicItemsParserHandler()] = $instance;
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/DynamicItem/AttributeKeyCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function parseManual(\Gettext\Translations $translations, $concrete5versi
$akcClass = '\Concrete\Core\Attribute\Key\Category';
}
if (class_exists($akcClass, true) && method_exists($akcClass, 'getList')) {
foreach (call_user_func($akcClass.'::getList') as $akc) {
foreach (call_user_func($akcClass . '::getList') as $akc) {
$akcHandle = $akc->getAttributeKeyCategoryHandle();
$this->addTranslation($translations, isset($akcNameMap[$akcHandle]) ? $akcNameMap[$akcHandle] : ucwords(str_replace(array('_', '-', '/'), ' ', $akcHandle)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/DynamicItem/DynamicItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final public function parse(\Gettext\Translations $translations, $concrete5versi
{
$fqClassName = $this->getClassNameForExtractor();
if (is_string($fqClassName) && ($fqClassName !== '') && class_exists($fqClassName, true) && method_exists($fqClassName, 'exportTranslations')) {
$translations->mergeWith(call_user_func($fqClassName.'::exportTranslations'));
$translations->mergeWith(call_user_func($fqClassName . '::exportTranslations'));
} else {
$this->parseManual($translations, $concrete5version);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/DynamicItem/PermissionKeyCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function parseManual(\Gettext\Translations $translations, $concrete5versi
$pkcClass = '\Concrete\Core\Permission\Category';
}
if (class_exists($pkcClass, true) && method_exists($pkcClass, 'getList')) {
foreach (call_user_func($pkcClass.'::getList') as $pkc) {
foreach (call_user_func($pkcClass . '::getList') as $pkc) {
$pkcHandle = $pkc->getPermissionKeyCategoryHandle();
$this->addTranslation($translations, isset($pkcNameMap[$pkcHandle]) ? $pkcNameMap[$pkcHandle] : ucwords(str_replace(array('_', '-', '/'), ' ', $pkcHandle)));
}
Expand Down
18 changes: 9 additions & 9 deletions src/Parser/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDi
$references = $newTranslation->getReferences();
$newTranslation->deleteReferences();
foreach ($references as $reference) {
$newTranslation->addReference($relativePath.'/'.$reference[0], $reference[1]);
$newTranslation->addReference($relativePath . '/' . $reference[0], $reference[1]);
}
$translations[] = $newTranslation;
}
Expand Down Expand Up @@ -125,14 +125,14 @@ protected static function parseDirectoryDo_xgettext($rootDirectory, $phpFiles)
throw new \Exception(t('Unable to create a temporary file'));
}
if (DIRECTORY_SEPARATOR === '\\') {
$line = 'cd /D '.escapeshellarg(str_replace('/', '\\', $rootDirectory));
$line = 'cd /D ' . escapeshellarg(str_replace('/', '\\', $rootDirectory));
} else {
$line = 'cd '.escapeshellarg($rootDirectory);
$line = 'cd ' . escapeshellarg($rootDirectory);
}
$line .= ' && xgettext';
$line .= ' --default-domain=messages'; // Domain
$line .= ' --output='.escapeshellarg(basename($tempFilePot)); // Output .pot file name
$line .= ' --output-dir='.escapeshellarg(dirname($tempFilePot)); // Output .pot folder name
$line .= ' --output=' . escapeshellarg(basename($tempFilePot)); // Output .pot file name
$line .= ' --output-dir=' . escapeshellarg(dirname($tempFilePot)); // Output .pot folder name
$line .= ' --language=PHP'; // Source files are in php
$line .= ' --from-code=UTF-8'; // Source files are in utf-8
$line .= ' --add-comments=i18n'; // Place comment blocks preceding keyword lines in output file if they start with '// i18n: '
Expand All @@ -142,7 +142,7 @@ protected static function parseDirectoryDo_xgettext($rootDirectory, $phpFiles)
$line .= ' --keyword=tc:1c,2'; // Look for the first argument of the "tc" function for extracting translation context, and the second argument is the translatable text in singular form.
$line .= ' --no-escape'; // Do not use C escapes in output
$line .= ' --add-location'; // Generate '#: filename:line' lines
$line .= ' --files-from='.escapeshellarg($tempFileList); // Get list of input files from file
$line .= ' --files-from=' . escapeshellarg($tempFileList); // Get list of input files from file
$line .= ' 2>&1';
$output = array();
$rc = null;
Expand All @@ -156,7 +156,7 @@ protected static function parseDirectoryDo_xgettext($rootDirectory, $phpFiles)
$output = array();
}
if ($rc !== 0) {
throw new \Exception('xgettext failed: '.implode("\n", $output));
throw new \Exception('xgettext failed: ' . implode("\n", $output));
}
$newTranslations = \Gettext\Translations::fromPoFile($tempFilePot);
@unlink($tempFilePot);
Expand Down Expand Up @@ -186,7 +186,7 @@ protected static function parseDirectoryDo_xgettext($rootDirectory, $phpFiles)
*/
protected static function parseDirectoryDo_php($rootDirectory, $phpFiles)
{
$prefix = $rootDirectory.'/';
$prefix = $rootDirectory . '/';
$originalExtractComments = \Gettext\Extractors\PhpCode::$extractComments;
\Gettext\Extractors\PhpCode::$extractComments = 'i18n';
$originalFunctions = \Gettext\Extractors\PhpCode::$functions;
Expand All @@ -196,7 +196,7 @@ protected static function parseDirectoryDo_php($rootDirectory, $phpFiles)
try {
$absFiles = array_map(
function ($phpFile) use ($prefix) {
return $prefix.$phpFile;
return $prefix . $phpFile;
},
$phpFiles
);
Expand Down
6 changes: 3 additions & 3 deletions src/Parser/ThemePresets.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDi
if ($dirList === false) {
throw new \Exception("Unable to parse directory $presetsAbsDirectory");
}
$shownChild = ($child === '') ? rtrim($prefix, '/') : ($prefix.$child);
$shownChild = ($child === '') ? rtrim($prefix, '/') : ($prefix . $child);
foreach ($dirList as $file) {
if (($file[0] !== '.') && preg_match('/[^.].*\.less$/i', $file)) {
$fileAbs = "$presetsAbsDirectory/$file";
Expand All @@ -63,7 +63,7 @@ function ($matches) {
$content
);
foreach (array("'", '"') as $quote) {
if (preg_match('%(?:^|\\n|;)[ \\t]*@preset-name:\\s*'.$quote.'([^'.$quote.']*)'.$quote.'\\s*(?:;|$)%s', $content, $matches)) {
if (preg_match('%(?:^|\\n|;)[ \\t]*@preset-name:\\s*' . $quote . '([^' . $quote . ']*)' . $quote . '\\s*(?:;|$)%s', $content, $matches)) {
$presetName = $matches[1];
$presetLine = null;
$p = strpos($content, $matches[0]);
Expand All @@ -73,7 +73,7 @@ function ($matches) {
if (!isset($themesPresets[$presetName])) {
$themesPresets[$presetName] = array();
}
$themesPresets[$presetName][] = array($shownChild."/$file", $presetLine);
$themesPresets[$presetName][] = array($shownChild . "/$file", $presetLine);
break;
}
}
Expand Down
Loading

0 comments on commit 4e7b43c

Please sign in to comment.