Skip to content

Commit

Permalink
Normalize array properties
Browse files Browse the repository at this point in the history
  • Loading branch information
VasekPurchart committed Jan 6, 2016
1 parent 3eaa085 commit c117fe1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
19 changes: 19 additions & 0 deletions SlevomatCodingStandard/Helpers/SniffSettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SlevomatCodingStandard\Sniffs\Classes;

use PHP_CodeSniffer_File;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\StringHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;

Expand All @@ -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[]
*/
Expand All @@ -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
Expand Down Expand Up @@ -180,15 +211,15 @@ 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;
}
}

$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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SlevomatCodingStandard\Sniffs\Files;

use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\StringHelper;

class TypeNameMatchesFileNameSniff implements \PHP_CodeSniffer_Sniff
Expand All @@ -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;

Expand All @@ -31,15 +41,51 @@ 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
*/
private function getNamespaceExtractor()
{
if ($this->namespaceExtractor === null) {
$this->namespaceExtractor = new FilepathNamespaceExtractor(
$this->rootNamespaces,
$this->skipDirs
$this->getRootNamespaces(),
$this->getSkipDirs()
);
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit c117fe1

Please sign in to comment.