AliasExtractor::extract()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
rs 9.4286
c 1
b 0
f 0
cc 3
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\ReflectedMethod;
12
use Hal\Component\Token\TokenCollection;
13
14
15
/**
16
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
17
 */
18
class AliasExtractor implements ExtractorInterface {
19
20
    /**
21
     * @var Searcher
22
     */
23
    private $searcher;
24
25
    /**
26
     * Constructor
27
     *
28
     * @param Searcher $searcher
29
     */
30
    public function __construct(Searcher $searcher)
31
    {
32
        $this->searcher = $searcher;
33
    }
34
35
    /**
36
     * Extract alias from position
37
     *
38
     * @param $n
39
     * @param TokenCollection $tokens
40
     * @return ReflectedMethod
41
     */
42
    public function extract(&$n, TokenCollection $tokens)
43
    {
44
        $alias = $real = null;
45
        $expression = $this->searcher->getUnder(array(';'), $n, $tokens);
46
        if(preg_match('!use\s+(.*)\s+as\s+(.*)!i', $expression, $matches)) {
47
            list(, $real, $alias) = $matches;
48
        } else if(preg_match('!use\s+([^\s\(]+)\s*!i', $expression, $matches)) {
49
            list(, $real) = $matches;
50
            $alias = $real;
51
        }
52
53
        return (object) array(
54
            'name' => $real
55
            , 'alias' => $alias
56
        );
57
    }
58
};
59