Skip to content

Commit

Permalink
AbstractPropertyTypePerClassLimit: decouple
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 12, 2016
1 parent f9dd696 commit ff193f0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
88 changes: 54 additions & 34 deletions src/ObjectCalisthenics/AbstractPropertyTypePerClassLimitSniff.php
Expand Up @@ -29,6 +29,16 @@ abstract class AbstractPropertyTypePerClassLimitSniff
*/
private $propertyList;

/**
* @var PHP_CodeSniffer_File
*/
private $phpcsFile;

/**
* @var int
*/
private $stackPtr;

public function register() : array
{
return [T_CLASS, T_TRAIT];
Expand All @@ -41,39 +51,20 @@ public function register() : array
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$this->propertyList = ClassAnalyzer::getClassProperties($phpcsFile, $stackPtr);
$this->phpcsFile = $phpcsFile;
$this->stackPtr = $stackPtr;

// Check for tracked property type amount
if (($error = $this->checkTrackedClassPropertyAmount()) !== '') {
$phpcsFile->addError($error, $stackPtr, 'TooManyTrackedProperties');

if ($this->checkTotalPropertiesAmount()) {
return;
}

// Check for each tracked property type amount
$errorList = $this->checkTrackedClassPropertyTypeAmount();

if ($errorList) {
array_map(
function ($error) use ($phpcsFile, $stackPtr) {
$phpcsFile->addError($error, $stackPtr, 'TooManyPropertiesOfType');
},
$errorList
);

if ($this->checkTrackedPropertiesAmount()) {
return;
}

$this->checkUntrackedPropertyTypeAmount($phpcsFile, $stackPtr);
}

/**
* @return string
*/
protected function getUntrackedPropertyType() : string
{
return 'untracked';
}

private function checkUntrackedPropertyTypeAmount(PHP_CodeSniffer_File $phpcsFile, int $stackPtr)
{
if (($error = $this->checkUntrackedClassPropertyAmount()) !== '') {
Expand Down Expand Up @@ -105,14 +96,19 @@ private function checkTrackedClassPropertyTypeAmount() : array
$segregatedPropertyList = $this->getClassPropertiesSegregatedByType();
$errorList = [];

foreach ($segregatedPropertyList as $propertyType => $propertyOfTypeList) {
$overLimitPropertyList = array_filter($segregatedPropertyList, function (array $propertyOfTypeList) {
$propertyOfTypeAmount = count($propertyOfTypeList);
if ($propertyOfTypeAmount > $this->trackedMaxCount) {
$message = 'You have %d properties of "%s" type, must be less or equals than %d properties in total';
$error = sprintf($message, $propertyOfTypeAmount, $propertyType, $this->trackedMaxCount);

$errorList[] = $error;
}
return $propertyOfTypeAmount > $this->trackedMaxCount;
});

foreach ($overLimitPropertyList as $propertyType => $propertyOfTypeList) {
$errorList[] = sprintf(
'You have %d properties of "%s" type, must be less or equals than %d properties in total',
count($propertyOfTypeList),
$propertyType,
$this->trackedMaxCount
);
}

return $errorList;
Expand All @@ -125,7 +121,7 @@ private function checkUntrackedClassPropertyAmount() : string

if ($untrackedPropertyAmount > $this->untrackedMaxCount) {
$message = 'You have %d properties declared of %s type, must be less or equals than %d properties in total';
$error = sprintf($message, $untrackedPropertyAmount, $this->getUntrackedPropertyType(), $this->untrackedMaxCount);
$error = sprintf($message, $untrackedPropertyAmount, 'object instance', $this->untrackedMaxCount);

return $error;
}
Expand All @@ -138,13 +134,37 @@ private function getClassPropertiesSegregatedByType() : array
$segregatedPropertyList = [];

foreach ($this->propertyList as $property) {
if (!isset($segregatedPropertyList[$property['type']])) {
$segregatedPropertyList[$property['type']] = [];
}

$segregatedPropertyList[$property['type']][] = $property;
}

return $segregatedPropertyList;
}

private function checkTotalPropertiesAmount() : bool
{
if (($error = $this->checkTrackedClassPropertyAmount()) !== '') {
$this->phpcsFile->addError($error, $this->stackPtr);

return true;
}

return false;
}

private function checkTrackedPropertiesAmount() : bool
{
$errorList = $this->checkTrackedClassPropertyTypeAmount();
if ($errorList) {
array_map(
function ($error) {
$this->phpcsFile->addError($error, $this->stackPtr, 'TooManyPropertiesOfType');
},
$errorList
);

return true;
}

return false;
}
}
Expand Up @@ -38,9 +38,4 @@ protected function getTrackedPropertyTypeList() : array
'string',
];
}

protected function getUntrackedPropertyType() : string
{
return 'object instance';
}
}

0 comments on commit ff193f0

Please sign in to comment.