Skip to content

Commit

Permalink
Merge branch 'feature/42' into develop
Browse files Browse the repository at this point in the history
Close #42
  • Loading branch information
michalbundyra committed Oct 25, 2019
2 parents af3a167 + db2f73d commit df93d48
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ All notable changes to this project will be documented in this file, in reverse

- [#39](https://github.com/webimpress/coding-standard/pull/39) adds `Arrays\DuplicateKey` sniff which detects duplicated keys in arrays

- [#42](https://github.com/webimpress/coding-standard/pull/42) adds requiring camelCase names for class members and variables used in strings - extended sniff `NamingConventions\ValidVariableName`.
Disallowed are two capital letters next to each other (strict mode).

### Changed

- Nothing.
- [#42](https://github.com/webimpress/coding-standard/pull/42) changes `NamingConventions\ValidVariableName` to require variable names be in strict camelCase. It means two capital letters next to each other are not allowed.

### Deprecated

- Nothing.

### Removed

- Nothing.
- [#42](https://github.com/webimpress/coding-standard/pull/42) excludes `PSR2.Classes.PropertyDeclaration.Underscore` check, as it is now covered by `NamingConventions\ValidVariableName` sniff

### Fixed

Expand Down
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

0 comments on commit df93d48

Please sign in to comment.