ClassExtractor::extract()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 11
nc 3
nop 2
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\OOP\Extractor;
11
use Hal\Component\OOP\Reflected\ReflectedClass\ReflectedAnonymousClass;
12
use Hal\Component\OOP\Reflected\ReflectedClass;
13
use Hal\Component\Token\TokenCollection;
14
15
16
/**
17
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
18
 */
19
class ClassExtractor implements ExtractorInterface {
20
21
    /**
22
     * @var Searcher
23
     */
24
    private $searcher;
25
26
    /**
27
     * @var string
28
     */
29
    private $namespace;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param Searcher $searcher
35
     */
36
    public function __construct(Searcher $searcher)
37
    {
38
        $this->searcher = $searcher;
39
    }
40
41
    /**
42
     * Extract class from position
43
     *
44
     * @param $n
45
     * @param TokenCollection $tokens
46
     * @return ReflectedClass
47
     */
48
    public function extract(&$n, TokenCollection $tokens)
49
    {
50
        // is PHP7 ?
51
        $previous = $tokens->get($n - 2);
52
        if($previous && T_NEW === $previous->getType()) {
53
            // anonymous class
54
            $class = new ReflectedAnonymousClass($this->namespace, 'class@anonymous');
55
            return $class;
56
        }
57
58
        // is abstract ?
59
        $prev = $this->searcher->getPrevious($n, $tokens);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $prev is correct as $this->searcher->getPrevious($n, $tokens) (which targets Hal\Component\OOP\Extrac...Searcher::getPrevious()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
60
        $isAbstract = $prev && T_ABSTRACT === $prev->getType();
61
62
        $classname = $this->searcher->getFollowingName($n, $tokens);
63
        $class = new ReflectedClass($this->namespace, trim($classname));
64
        $class->setAbstract($isAbstract);
65
        return $class;
66
    }
67
68
    /**
69
     * @param string $namespace
70
     */
71
    public function setNamespace($namespace)
72
    {
73
        $this->namespace = (string) $namespace;
74
    }
75
76
};