Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: all variables must be camel case - strict #42

Merged
merged 5 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/WebimpressCodingStandard/Helper/MethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,13 @@ private function typesMatch(string $typeHint, string $typeStr) : bool
return false;
}

$fqcnTypeHint = strtolower($this->getFQCN($lowerTypeHint));
$fqcnTypeHint = strtolower($this->getFqcn($lowerTypeHint));
foreach ($types as $key => $type) {
if ($type === 'null') {
continue;
}

$types[$key] = strtolower($this->getFQCN($type));
$types[$key] = strtolower($this->getFqcn($type));
}
$fqcnTypes = implode('|', $types);

Expand All @@ -279,7 +279,7 @@ private function typesMatch(string $typeHint, string $typeStr) : bool
|| $fqcnTypeHint . '|null' === $fqcnTypes));
}

private function getFQCN(string $class) : string
private function getFqcn(string $class) : string
{
// It is a simple type
if (in_array(strtolower($class), $this->simpleReturnTypes, true)) {
Expand Down Expand Up @@ -308,10 +308,10 @@ private function isClassName(string $name) : bool

$ns = strtolower($this->currentNamespace);
$lowerClassName = strtolower($this->className);
$lowerFQCN = ($ns ? '\\' . $ns : '') . '\\' . $lowerClassName;
$lowerFqcn = ($ns ? '\\' . $ns : '') . '\\' . $lowerClassName;
$lower = strtolower($name);

return $lower === $lowerFQCN
return $lower === $lowerFqcn
|| $lower === $lowerClassName;
}

Expand All @@ -322,10 +322,10 @@ private function isParentClassName(string $name) : bool
}

$lowerParentClassName = strtolower($this->parentClassName);
$lowerFQCN = strtolower($this->getFQCN($lowerParentClassName));
$lowerFqcn = strtolower($this->getFqcn($lowerParentClassName));
$lower = strtolower($name);

return $lower === $lowerFQCN
return $lower === $lowerFqcn
|| $lower === $lowerParentClassName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHP_CodeSniffer\Util\Common;

use function ltrim;
use function preg_match_all;

use const T_DOUBLE_COLON;
use const T_WHITESPACE;
Expand Down Expand Up @@ -48,8 +49,8 @@ protected function processVariable(File $phpcsFile, $stackPtr) : void
return; // skip MyClass::$variable, there might be no control over the declaration
}

if (! Common::isCamelCaps($varName, false, true, false)) {
$error = 'Variable "%s" is not in valid camel caps format';
if (! Common::isCamelCaps($varName, false, true, true)) {
$error = 'Variable "$%s" is not in valid camel caps format';
$data = [$varName];
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
}
Expand All @@ -60,14 +61,33 @@ protected function processVariable(File $phpcsFile, $stackPtr) : void
*/
protected function processMemberVar(File $phpcsFile, $stackPtr) : void
{
// handled by PSR2.Classes.PropertyDeclaration
$tokens = $phpcsFile->getTokens();
$varName = ltrim($tokens[$stackPtr]['content'], '$');

if (! Common::isCamelCaps($varName, false, true, true)) {
$error = 'Property "$%s" is not in valid camel caps format';
$data = [$varName];
$phpcsFile->addError($error, $stackPtr, 'NotCamelCapsProperty', $data);
}
}

/**
* @param int $stackPtr
*/
protected function processVariableInString(File $phpcsFile, $stackPtr) : void
{
// handled by Squiz.Strings.DoubleQuoteUsage
$tokens = $phpcsFile->getTokens();
$content = $tokens[$stackPtr]['content'];

$pattern = '|(?<!\\\\)(?:\\\\{2})*\${?([a-zA-Z0-9_]+)}?|';
preg_match_all($pattern, $content, $matches);

foreach ($matches[1] ?? [] as $varName) {
if (! Common::isCamelCaps($varName, false, true, true)) {
$error = 'Variable "$%s" is not in valid camel caps format';
$data = [$varName];
$phpcsFile->addError($error, $stackPtr, 'NotCamelCapsInString', $data);
}
}
}
}
2 changes: 2 additions & 0 deletions src/WebimpressCodingStandard/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<!-- Replaced by WebimpressCodingStandard.WhiteSpace.CommaSpacing -->
<exclude name="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpaceBeforeComma"/>
<exclude name="Squiz.Functions.FunctionDeclarationArgumentSpacing.NoSpaceBeforeArg"/>
<!-- Replaced by WebimpressCodingStandard.NamingConventions.ValidVariableName -->
<exclude name="PSR2.Classes.PropertyDeclaration.Underscore"/>
</rule>

<!-- Generic Rules -->
Expand Down
9 changes: 7 additions & 2 deletions test/Sniffs/NamingConventions/ValidVariableNameUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ echo \Library::$_variable;
echo \Library::$_another_variable;

class Foo {
protected $_this_is_not_handled_by_this_sniff;
protected $_invalid;
protected $userID;
}

$string = "This $_some_variable is not {$handled_by} this sniff.";
$string = "This {$_some_variable} is now ${handled_by} this $sniff.";
$string .= $_some_variable;
$string .= $camelCase;
$string .= "{$myIP}";

$userID = 'invalid due to two uppercase characters next to each other';
$InvalidVariable = "as first letter cannot be uppercase";
8 changes: 7 additions & 1 deletion test/Sniffs/NamingConventions/ValidVariableNameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ protected function getErrorList(string $testFile = '') : array
return [
15 => 1,
16 => 1,
28 => 1,
24 => 1,
25 => 1,
28 => 2,
29 => 1,
31 => 1,
33 => 1,
34 => 1,
];
}

Expand Down