Changeset View
Changeset View
Standalone View
Standalone View
arcanist/linter/ShellLocaleLinter.php
<?php | <?php | ||||
Lint: Generic.PHP.RequireStrictTypes.MissingDeclaration: Missing required strict_types declaration | |||||
FabienUnsubmitted Done Inline ActionsYou should have a very recent phpcs because this rule is 3 months old according to github: It enforces using the new PHP 7 strict_types declaration: https://www.php.net/manual/fr/control-structures.declare.php. Because none of our code nor arcanist code is compliant, an exception can be added to the rule file (arcanist/phpcs.xml). Fabien: You should have a very recent `phpcs` because this rule is 3 months old according to github… | |||||
jasonbcoxAuthorUnsubmitted Done Inline Actionsjasonbcox: D4193 | |||||
/** | /** | ||||
* Enforce using `export LC_ALL=C` in shell scripts. | * Enforce using `export LC_ALL=C` or `export LC_ALL=C.UTF-8` in shell scripts. | ||||
*/ | */ | ||||
final class ShellLocaleLinter extends ArcanistLinter { | final class ShellLocaleLinter extends ArcanistLinter { | ||||
const INVALID_LOCALE = 1; | const INVALID_LOCALE = 1; | ||||
const LOCALE_STATEMENT = 'export LC_ALL=C'; | const LOCALE_STATEMENTS = array('export LC_ALL=C', 'export LC_ALL=C.UTF-8'); | ||||
public function getInfoName() { | public function getInfoName() { | ||||
return 'lint-shell-locale'; | return 'lint-shell-locale'; | ||||
} | } | ||||
public function getInfoDescription() { | public function getInfoDescription() { | ||||
return pht('Enforce using `'.self::LOCALE_STATEMENT.'` in shell scripts.'); | return pht('Enforce using `'.join('` or `', self::LOCALE_STATEMENTS). | ||||
'` in shell scripts.'); | |||||
} | } | ||||
public function getLinterName() { | public function getLinterName() { | ||||
return 'SHELL_LOCALE'; | return 'SHELL_LOCALE'; | ||||
} | } | ||||
public function getLinterConfigurationName() { | public function getLinterConfigurationName() { | ||||
return 'lint-shell-locale'; | return 'lint-shell-locale'; | ||||
} | } | ||||
public function getLintSeverityMap() { | public function getLintSeverityMap() { | ||||
return array( | return array( | ||||
self::INVALID_LOCALE => ArcanistLintSeverity::SEVERITY_ERROR, | self::INVALID_LOCALE => ArcanistLintSeverity::SEVERITY_ERROR, | ||||
); | ); | ||||
} | } | ||||
public function getLintNameMap() { | public function getLintNameMap() { | ||||
return array( | return array( | ||||
self::INVALID_LOCALE => pht('`'.self::LOCALE_STATEMENT.'` should be '. | self::INVALID_LOCALE => pht( | ||||
'`'.join('` or `', self::LOCALE_STATEMENTS).'` should be '. | |||||
'the first statement.'), | 'the first statement.'), | ||||
); | ); | ||||
} | } | ||||
public function lintPath($path) { | public function lintPath($path) { | ||||
$absPath = Filesystem::resolvePath($path, $this->getProjectRoot()); | $absPath = Filesystem::resolvePath($path, $this->getProjectRoot()); | ||||
$fileContent = Filesystem::readFile($absPath); | $fileContent = Filesystem::readFile($absPath); | ||||
if (!preg_match_all('/^[^#\s]+.+/m', $fileContent, $matches)) { | if (!preg_match_all('/^[^#\s]+.+/m', $fileContent, $matches)) { | ||||
throw new Exception(pht('Error while parsing %s: the script seems to '. | throw new Exception(pht('Error while parsing %s: the script seems to '. | ||||
'have only comments and/or empty lines.', $path)); | 'have only comments and/or empty lines.', $path)); | ||||
} | } | ||||
if (!(trim($matches[0][0]) === self::LOCALE_STATEMENT)) { | $firstMatch = trim($matches[0][0]); | ||||
foreach(self::LOCALE_STATEMENTS as $statement) { | |||||
if ($firstMatch === $statement) { | |||||
return; | |||||
} | |||||
} | |||||
return $this->raiseLintAtPath( | return $this->raiseLintAtPath( | ||||
self::INVALID_LOCALE, | self::INVALID_LOCALE, | ||||
pht('Shell scripts should set the locale to avoid side effects.') | pht('Shell scripts should set the locale to avoid side effects.') | ||||
); | ); | ||||
FabienUnsubmitted Done Inline ActionsPHP has the array_search() built-in for doing this: if (array_search(trim($matches[0][0]), self::LOCALE_STATEMENTS, true) !== false) { return $this->raiseLintAtPath( self::INVALID_LOCALE, pht('Shell scripts should set the locale to avoid side effects.') ); } Fabien: PHP has the `array_search()` built-in for doing this:
```lang=php
if (array_search(trim… | |||||
jasonbcoxAuthorUnsubmitted Done Inline Actionscool. equality is flipped though ;) jasonbcox: cool. equality is flipped though ;) | |||||
FabienUnsubmitted Not Done Inline ActionsOops... good catch ! Fabien: Oops... good catch ! | |||||
} | } | ||||
} | } | ||||
} |
Missing required strict_types declaration