From 3329fbac5fedbd742347a5e3249b59c06e38dab4 Mon Sep 17 00:00:00 2001 From: Vasek Purchart Date: Wed, 6 Jan 2016 16:36:50 +0100 Subject: [PATCH] Normalize array properties --- .../Helpers/SniffSettingsHelper.php | 19 +++++++ .../Classes/UnusedPrivateElementsSniff.php | 35 ++++++++++++- .../Files/TypeNameMatchesFileNameSniff.php | 52 +++++++++++++++++-- 3 files changed, 101 insertions(+), 5 deletions(-) diff --git a/SlevomatCodingStandard/Helpers/SniffSettingsHelper.php b/SlevomatCodingStandard/Helpers/SniffSettingsHelper.php index 9e77180a8..d2fca1905 100644 --- a/SlevomatCodingStandard/Helpers/SniffSettingsHelper.php +++ b/SlevomatCodingStandard/Helpers/SniffSettingsHelper.php @@ -20,4 +20,23 @@ public static function normalizeArray(array $settings) return array_values($settings); } + /** + * @param mixed[] $settings + * @return mixed[] + */ + public static function normalizeAssociativeArray(array $settings) + { + $normalizedSettings = []; + foreach ($settings as $key => $value) { + $key = trim($key); + $value = trim($value); + if ($key === '' || $value === '') { + continue; + } + $normalizedSettings[$key] = $value; + } + + return $normalizedSettings; + } + } diff --git a/SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php b/SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php index 0fcbaaece..45a5f62c9 100644 --- a/SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php +++ b/SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php @@ -3,6 +3,7 @@ namespace SlevomatCodingStandard\Sniffs\Classes; use PHP_CodeSniffer_File; +use SlevomatCodingStandard\Helpers\SniffSettingsHelper; use SlevomatCodingStandard\Helpers\StringHelper; use SlevomatCodingStandard\Helpers\TokenHelper; @@ -18,9 +19,15 @@ class UnusedPrivateElementsSniff implements \PHP_CodeSniffer_Sniff /** @var string[] */ public $alwaysUsedPropertiesAnnotations = []; + /** @var string[] */ + private $normalizedAlwaysUsedPropertiesAnnotations; + /** @var string[] */ public $alwaysUsedPropertiesSuffixes = []; + /** @var string[] */ + private $normalizedAlwaysUsedPropertiesSuffixes; + /** * @return integer[] */ @@ -31,6 +38,30 @@ public function register() ]; } + /** + * @return string[] + */ + private function getAlwaysUsedPropertiesAnnotations() + { + if ($this->normalizedAlwaysUsedPropertiesAnnotations === null) { + $this->normalizedAlwaysUsedPropertiesAnnotations = SniffSettingsHelper::normalizeArray($this->alwaysUsedPropertiesAnnotations); + } + + return $this->normalizedAlwaysUsedPropertiesAnnotations; + } + + /** + * @return string[] + */ + private function getAlwaysUsedPropertiesSuffixes() + { + if ($this->normalizedAlwaysUsedPropertiesSuffixes === null) { + $this->normalizedAlwaysUsedPropertiesSuffixes = SniffSettingsHelper::normalizeArray($this->alwaysUsedPropertiesSuffixes); + } + + return $this->normalizedAlwaysUsedPropertiesSuffixes; + } + /** * @param \PHP_CodeSniffer_File $phpcsFile * @param integer $classPointer @@ -180,7 +211,7 @@ private function getProperties(PHP_CodeSniffer_File $phpcsFile, array $tokens, a $phpDocTags = $this->getPhpDocTags($phpcsFile, $tokens, $visibilityModifiedTokenPointer); foreach ($phpDocTags as $tag) { preg_match('#([@a-zA-Z\\\]+)#', $tag, $matches); - if (in_array($matches[1], $this->alwaysUsedPropertiesAnnotations, true)) { + if (in_array($matches[1], $this->getAlwaysUsedPropertiesAnnotations(), true)) { continue 2; } } @@ -188,7 +219,7 @@ private function getProperties(PHP_CodeSniffer_File $phpcsFile, array $tokens, a $propertyToken = $tokens[$propertyTokenPointer]; $name = substr($propertyToken['content'], 1); - foreach ($this->alwaysUsedPropertiesSuffixes as $prefix) { + foreach ($this->getAlwaysUsedPropertiesSuffixes() as $prefix) { if (StringHelper::endsWith($name, $prefix)) { continue 2; } diff --git a/SlevomatCodingStandard/Sniffs/Files/TypeNameMatchesFileNameSniff.php b/SlevomatCodingStandard/Sniffs/Files/TypeNameMatchesFileNameSniff.php index 0310dd1ba..2c99e4697 100644 --- a/SlevomatCodingStandard/Sniffs/Files/TypeNameMatchesFileNameSniff.php +++ b/SlevomatCodingStandard/Sniffs/Files/TypeNameMatchesFileNameSniff.php @@ -2,6 +2,7 @@ namespace SlevomatCodingStandard\Sniffs\Files; +use SlevomatCodingStandard\Helpers\SniffSettingsHelper; use SlevomatCodingStandard\Helpers\StringHelper; class TypeNameMatchesFileNameSniff implements \PHP_CodeSniffer_Sniff @@ -10,12 +11,21 @@ class TypeNameMatchesFileNameSniff implements \PHP_CodeSniffer_Sniff /** @var string[] path(string) => namespace */ public $rootNamespaces = []; + /** @var string[] path(string) => namespace */ + private $normalizedRootNamespaces; + /** @var string */ public $skipDirs = []; + /** @var string */ + private $normalizedSkipDirs; + /** @var string[] */ public $ignoredNamespaces = []; + /** @var string[] */ + private $normalizedIgnoredNamespaces; + /** @var \SlevomatCodingStandard\Sniffs\Files\FilepathNamespaceExtractor */ private $namespaceExtractor; @@ -31,6 +41,42 @@ public function register() ]; } + /** + * @return string[] path(string) => namespace + */ + private function getRootNamespaces() + { + if ($this->normalizedRootNamespaces === null) { + $this->normalizedRootNamespaces = SniffSettingsHelper::normalizeAssociativeArray($this->rootNamespaces); + } + + return $this->normalizedRootNamespaces; + } + + /** + * @return string[] + */ + private function getSkipDirs() + { + if ($this->normalizedSkipDirs === null) { + $this->normalizedSkipDirs = SniffSettingsHelper::normalizeArray($this->skipDirs); + } + + return $this->normalizedSkipDirs; + } + + /** + * @return string[] + */ + private function getIgnoredNamespaces() + { + if ($this->normalizedIgnoredNamespaces === null) { + $this->normalizedIgnoredNamespaces = SniffSettingsHelper::normalizeArray($this->ignoredNamespaces); + } + + return $this->normalizedIgnoredNamespaces; + } + /** * @return \SlevomatCodingStandard\Sniffs\Files\FilepathNamespaceExtractor */ @@ -38,8 +84,8 @@ private function getNamespaceExtractor() { if ($this->namespaceExtractor === null) { $this->namespaceExtractor = new FilepathNamespaceExtractor( - $this->rootNamespaces, - $this->skipDirs + $this->getRootNamespaces(), + $this->getSkipDirs() ); } @@ -78,7 +124,7 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPointer) return; } - foreach ($this->ignoredNamespaces as $ignoredNamespace) { + foreach ($this->getIgnoredNamespaces() as $ignoredNamespace) { if (StringHelper::startsWith($typeName, $ignoredNamespace . '\\')) { return; }