Passed
Push — master ( 17dafe...3e29f4 )
by Gytis
02:42 queued 11s
created

FqcnDescriptionSniff::process()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 8
c 1
b 0
f 0
nc 16
nop 2
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace Gskema\TypeSniff\Sniffs\CodeElement;
4
5
use Gskema\TypeSniff\Core\CodeElement\Element\ClassElement;
6
use Gskema\TypeSniff\Core\CodeElement\Element\CodeElementInterface;
7
use Gskema\TypeSniff\Core\CodeElement\Element\InterfaceElement;
8
use Gskema\TypeSniff\Core\CodeElement\Element\TraitElement;
9
use PHP_CodeSniffer\Files\File;
10
11
class FqcnDescriptionSniff implements CodeElementSniffInterface
12
{
13
    /** @var string[] */
14
    protected $invalidPatterns = [
15
        '^(Class|Trait|Interface)\s+\w+\s*$',
16
    ];
17
18
    /** @var string[] */
19
    protected $invalidTags = [
20
        '@package'
21
    ];
22
23
    /**
24
     * @inheritDoc
25
     */
26 6
    public function configure(array $config): void
27
    {
28 6
        $this->invalidPatterns = array_merge($this->invalidPatterns, $config['invalidPatterns'] ?? []);
29 6
        foreach ($this->invalidPatterns as &$invalidPattern) {
30 6
            $invalidPattern = '#'.$invalidPattern.'#i';
31
        }
32
33 6
        $this->invalidTags = array_merge($this->invalidTags, $config['invalidTags'] ?? []);
34 6
        foreach ($this->invalidTags as &$invalidTag) {
35 6
             $invalidTag = substr($invalidTag, 1);
36
        }
37 6
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42 6
    public function register(): array
43
    {
44
        return [
45 6
            ClassElement::class,
46
            InterfaceElement::class,
47
            TraitElement::class,
48
        ];
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54 6
    public function process(File $file, CodeElementInterface $element): void
55
    {
56 6
        foreach ($element->getDocBlock()->getDescriptionLines() as $lineNum => $descriptionLine) {
57 1
            foreach ($this->invalidPatterns as $invalidPattern) {
58 1
                if (preg_match($invalidPattern, $descriptionLine)) {
59 1
                    $file->addWarningOnLine('Useless description', $lineNum, 'FqcnDescriptionSniff');
60
                }
61
            }
62
        }
63
64 6
        foreach ($element->getDocBlock()->getTags() as $tag) {
65 1
            foreach ($this->invalidTags as $invalidTagName) {
66 1
                if ($tag->getName() === $invalidTagName) {
67 1
                    $file->addWarningOnLine('Useless tag', $tag->getLine(), 'FqcnDescriptionSniff');
68
                }
69
            }
70
        }
71 6
    }
72
}
73