-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Gskema\TypeSniff\Sniffs\CodeElement; | ||
|
||
use Error; | ||
use Gskema\TypeSniff\Core\CodeElement\Element\AbstractFqcnElement; | ||
use Gskema\TypeSniff\Core\CodeElement\Element\ClassElement; | ||
use Gskema\TypeSniff\Core\CodeElement\Element\CodeElementInterface; | ||
use Gskema\TypeSniff\Core\SniffHelper; | ||
use IteratorAggregate; | ||
use PHP_CodeSniffer\Files\File; | ||
use ReflectionClass; | ||
use ReflectionException; | ||
|
||
class IteratorItemTypeSniff implements CodeElementSniffInterface | ||
{ | ||
protected const CODE = 'IteratorItemTypeSniff'; | ||
|
||
protected string $reportType = 'warning'; | ||
protected bool $addViolationId = true; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function configure(array $config): void | ||
{ | ||
$this->reportType = (string)($config['reportType'] ?? 'warning'); | ||
$this->addViolationId = (bool)($config['addViolationId'] ?? true); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function register(): array | ||
{ | ||
return [ | ||
ClassElement::class, | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @param AbstractFqcnElement $element | ||
*/ | ||
public function process(File $file, CodeElementInterface $element, CodeElementInterface $parentElement): void | ||
{ | ||
try { | ||
$ref = new ReflectionClass($element->getFqcn()); | ||
} catch (Error | ReflectionException) { | ||
return; // give up... | ||
} | ||
|
||
if (!in_array(IteratorAggregate::class, $ref->getInterfaceNames())) { | ||
return; | ||
} | ||
|
||
if ($element->getDocBlock()->getTagsByName('template-implements')) { | ||
return; | ||
} elseif ($element->getDocBlock()->getTagsByName('template-extends')) { | ||
return; | ||
} else { | ||
$originId = $this->addViolationId ? $element->getFqcn() : null; | ||
SniffHelper::addViolation( | ||
$file, | ||
'Classes which implement IteratorAggregate must have "@template-implements IteratorAggregate<?>"' | ||
. ' doc tag with a specified item type or template type', | ||
$element->getLine(), | ||
static::CODE, | ||
$this->reportType, | ||
$originId | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Gskema\TypeSniff\Sniffs\fixtures; | ||
|
||
use ArrayIterator; | ||
use IteratorAggregate; | ||
use Traversable; | ||
|
||
class TestIterator0 implements IteratorAggregate | ||
{ | ||
public function getIterator(): Traversable | ||
{ | ||
return new ArrayIterator([]); | ||
} | ||
} |