Skip to content

Commit c117fe1

Browse files
committed
Normalize array properties
1 parent 3eaa085 commit c117fe1

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

SlevomatCodingStandard/Helpers/SniffSettingsHelper.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,23 @@ public static function normalizeArray(array $settings)
2020
return array_values($settings);
2121
}
2222

23+
/**
24+
* @param mixed[] $settings
25+
* @return mixed[]
26+
*/
27+
public static function normalizeAssociativeArray(array $settings)
28+
{
29+
$normalizedSettings = [];
30+
foreach ($settings as $key => $value) {
31+
$key = trim($key);
32+
$value = trim($value);
33+
if ($key === '' || $value === '') {
34+
continue;
35+
}
36+
$normalizedSettings[$key] = $value;
37+
}
38+
39+
return $normalizedSettings;
40+
}
41+
2342
}

SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SlevomatCodingStandard\Sniffs\Classes;
44

55
use PHP_CodeSniffer_File;
6+
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
67
use SlevomatCodingStandard\Helpers\StringHelper;
78
use SlevomatCodingStandard\Helpers\TokenHelper;
89

@@ -18,9 +19,15 @@ class UnusedPrivateElementsSniff implements \PHP_CodeSniffer_Sniff
1819
/** @var string[] */
1920
public $alwaysUsedPropertiesAnnotations = [];
2021

22+
/** @var string[] */
23+
private $normalizedAlwaysUsedPropertiesAnnotations;
24+
2125
/** @var string[] */
2226
public $alwaysUsedPropertiesSuffixes = [];
2327

28+
/** @var string[] */
29+
private $normalizedAlwaysUsedPropertiesSuffixes;
30+
2431
/**
2532
* @return integer[]
2633
*/
@@ -31,6 +38,30 @@ public function register()
3138
];
3239
}
3340

41+
/**
42+
* @return string[]
43+
*/
44+
private function getAlwaysUsedPropertiesAnnotations()
45+
{
46+
if ($this->normalizedAlwaysUsedPropertiesAnnotations === null) {
47+
$this->normalizedAlwaysUsedPropertiesAnnotations = SniffSettingsHelper::normalizeArray($this->alwaysUsedPropertiesAnnotations);
48+
}
49+
50+
return $this->normalizedAlwaysUsedPropertiesAnnotations;
51+
}
52+
53+
/**
54+
* @return string[]
55+
*/
56+
private function getAlwaysUsedPropertiesSuffixes()
57+
{
58+
if ($this->normalizedAlwaysUsedPropertiesSuffixes === null) {
59+
$this->normalizedAlwaysUsedPropertiesSuffixes = SniffSettingsHelper::normalizeArray($this->alwaysUsedPropertiesSuffixes);
60+
}
61+
62+
return $this->normalizedAlwaysUsedPropertiesSuffixes;
63+
}
64+
3465
/**
3566
* @param \PHP_CodeSniffer_File $phpcsFile
3667
* @param integer $classPointer
@@ -180,15 +211,15 @@ private function getProperties(PHP_CodeSniffer_File $phpcsFile, array $tokens, a
180211
$phpDocTags = $this->getPhpDocTags($phpcsFile, $tokens, $visibilityModifiedTokenPointer);
181212
foreach ($phpDocTags as $tag) {
182213
preg_match('#([@a-zA-Z\\\]+)#', $tag, $matches);
183-
if (in_array($matches[1], $this->alwaysUsedPropertiesAnnotations, true)) {
214+
if (in_array($matches[1], $this->getAlwaysUsedPropertiesAnnotations(), true)) {
184215
continue 2;
185216
}
186217
}
187218

188219
$propertyToken = $tokens[$propertyTokenPointer];
189220
$name = substr($propertyToken['content'], 1);
190221

191-
foreach ($this->alwaysUsedPropertiesSuffixes as $prefix) {
222+
foreach ($this->getAlwaysUsedPropertiesSuffixes() as $prefix) {
192223
if (StringHelper::endsWith($name, $prefix)) {
193224
continue 2;
194225
}

SlevomatCodingStandard/Sniffs/Files/TypeNameMatchesFileNameSniff.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SlevomatCodingStandard\Sniffs\Files;
44

5+
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
56
use SlevomatCodingStandard\Helpers\StringHelper;
67

78
class TypeNameMatchesFileNameSniff implements \PHP_CodeSniffer_Sniff
@@ -10,12 +11,21 @@ class TypeNameMatchesFileNameSniff implements \PHP_CodeSniffer_Sniff
1011
/** @var string[] path(string) => namespace */
1112
public $rootNamespaces = [];
1213

14+
/** @var string[] path(string) => namespace */
15+
private $normalizedRootNamespaces;
16+
1317
/** @var string */
1418
public $skipDirs = [];
1519

20+
/** @var string */
21+
private $normalizedSkipDirs;
22+
1623
/** @var string[] */
1724
public $ignoredNamespaces = [];
1825

26+
/** @var string[] */
27+
private $normalizedIgnoredNamespaces;
28+
1929
/** @var \SlevomatCodingStandard\Sniffs\Files\FilepathNamespaceExtractor */
2030
private $namespaceExtractor;
2131

@@ -31,15 +41,51 @@ public function register()
3141
];
3242
}
3343

44+
/**
45+
* @return string[] path(string) => namespace
46+
*/
47+
private function getRootNamespaces()
48+
{
49+
if ($this->normalizedRootNamespaces === null) {
50+
$this->normalizedRootNamespaces = SniffSettingsHelper::normalizeAssociativeArray($this->rootNamespaces);
51+
}
52+
53+
return $this->normalizedRootNamespaces;
54+
}
55+
56+
/**
57+
* @return string[]
58+
*/
59+
private function getSkipDirs()
60+
{
61+
if ($this->normalizedSkipDirs === null) {
62+
$this->normalizedSkipDirs = SniffSettingsHelper::normalizeArray($this->skipDirs);
63+
}
64+
65+
return $this->normalizedSkipDirs;
66+
}
67+
68+
/**
69+
* @return string[]
70+
*/
71+
private function getIgnoredNamespaces()
72+
{
73+
if ($this->normalizedIgnoredNamespaces === null) {
74+
$this->normalizedIgnoredNamespaces = SniffSettingsHelper::normalizeArray($this->ignoredNamespaces);
75+
}
76+
77+
return $this->normalizedIgnoredNamespaces;
78+
}
79+
3480
/**
3581
* @return \SlevomatCodingStandard\Sniffs\Files\FilepathNamespaceExtractor
3682
*/
3783
private function getNamespaceExtractor()
3884
{
3985
if ($this->namespaceExtractor === null) {
4086
$this->namespaceExtractor = new FilepathNamespaceExtractor(
41-
$this->rootNamespaces,
42-
$this->skipDirs
87+
$this->getRootNamespaces(),
88+
$this->getSkipDirs()
4389
);
4490
}
4591

@@ -78,7 +124,7 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPointer)
78124
return;
79125
}
80126

81-
foreach ($this->ignoredNamespaces as $ignoredNamespace) {
127+
foreach ($this->getIgnoredNamespaces() as $ignoredNamespace) {
82128
if (StringHelper::startsWith($typeName, $ignoredNamespace . '\\')) {
83129
return;
84130
}

0 commit comments

Comments
 (0)