Completed
Push — master ( b11a4b...fca11f )
by Tobias
02:04
created

BaseVisitor::addError()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0488

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 14
cts 16
cp 0.875
rs 8.7624
cc 5
eloc 13
nc 6
nop 2
crap 5.0488
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Extractor\Visitor;
13
14
use Doctrine\Common\Annotations\DocParser;
15
use PhpParser\Node;
16
use Symfony\Component\Finder\SplFileInfo;
17
use Translation\Extractor\Annotation\Ignore;
18
use Translation\Extractor\Model\Error;
19
use Translation\Extractor\Model\SourceCollection;
20
21
/**
22
 * Base class for any visitor.
23
 *
24
 * @author Tobias Nyholm <[email protected]>
25
 */
26
abstract class BaseVisitor implements Visitor
27
{
28
    /**
29
     * @var SourceCollection
30
     */
31
    protected $collection;
32
33
    /**
34
     * @var SplFileInfo
35
     */
36
    protected $file;
37
38
    /**
39
     * @var DocParser
40
     */
41
    private $docParser;
42
43 22
    public function init(SourceCollection $collection, SplFileInfo $file)
44
    {
45 22
        $this->collection = $collection;
46 22
        $this->file = $file;
47 22
    }
48
49 20
    protected function getAbsoluteFilePath()
50
    {
51 20
        return $this->file->getRealPath();
52
    }
53
54
    /**
55
     * @param Node   $node
56
     * @param string $errorMessage
57
     */
58 7
    protected function addError(Node $node, $errorMessage)
59
    {
60 7
        $docComment = $node->getDocComment();
61 7
        $file = $this->getAbsoluteFilePath();
62
63 7
        if (property_exists($node, 'value')) {
64 5
            $line = $node->value->getAttribute('startLine');
0 ignored issues
show
Bug introduced by
Accessing value on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
65 5
        } else {
66 2
            $line = $node->getAttribute('startLine');
67
        }
68 7
        if (null !== $docComment) {
69 3
            $context = 'file '.$file.' near line '.$line;
70 3
            foreach ($this->getDocParser()->parse($docComment->getText(), $context) as $annotation) {
71 3
                if ($annotation instanceof Ignore) {
72 3
                    return;
73
                }
74
            }
75
        }
76
77 7
        $this->collection->addError(new Error($errorMessage, $file, $line));
78 7
    }
79
80
    /**
81
     * @return DocParser
82
     */
83 3
    private function getDocParser()
84
    {
85 3
        if (null === $this->docParser) {
86 3
            $this->docParser = new DocParser();
87
88 3
            $this->docParser->setImports([
89 3
                'ignore' => Ignore::class,
90 3
            ]);
91 3
            $this->docParser->setIgnoreNotImportedAnnotations(true);
92 3
        }
93
94 3
        return $this->docParser;
95
    }
96
97
    /**
98
     * @param DocParser $docParser
99
     */
100
    public function setDocParser(DocParser $docParser)
101
    {
102
        $this->docParser = $docParser;
103
    }
104
}
105