Completed
Pull Request — master (#221)
by personal
04:09
created

MethodUsageParser::parse()   C

Complexity

Conditions 11
Paths 35

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 21
nc 35
nop 1
dl 0
loc 36
rs 5.2653
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Parser\CodeParser;
11
12
use Hal\Component\Reflected\Method;
13
use Hal\Component\Parser\Resolver\NamespaceResolver;
14
use Hal\Component\Parser\Searcher;
15
use Hal\Component\Reflected\MethodUsage;
16
17
class MethodUsageParser
18
{
19
20
    /**
21
     * @var Searcher
22
     */
23
    private $searcher;
24
25
    /**
26
     * @var NamespaceResolver
27
     */
28
    private $namespaceResolver;
29
30
    /**
31
     * CodeParser constructor.
32
     * @param Searcher $searcher
33
     * @param NamespaceResolver $namespaceResolver
34
     */
35
    public function __construct(Searcher $searcher, NamespaceResolver $namespaceResolver)
36
    {
37
        $this->searcher = $searcher;
38
        $this->namespaceResolver = $namespaceResolver;
39
    }
40
41
42
    /**
43
     * @param Method $method
44
     * @return int
45
     */
46
    public function parse(Method $method) {
47
48
        $start = $this->searcher->getNext($method->getTokens(), 0, '{') + 1;
49
        $len = sizeof($method->getTokens());
50
        $tokens = array_slice($method->getTokens(), $start,  ($len - $start) - 1);
51
52
        foreach($tokens as $n => $token) {
53
            // replace $this->aaa by "class_attribute
54
            if(preg_match('!^\$this\->\w+$!', $token)) {
55
                $tokens[$n] = 'class_attribute';
56
            }
57
58
            // replace vars by "var"
59
            if(preg_match('!^\$\w+$!', $token) && $token != '$this') {
60
                $tokens[$n] = 'var';
61
            }
62
        }
63
        switch($tokens) {
64
            // getters
65
            case array('return', 'cast', 'class_attribute'):
66
            case array('return','class_attribute'):
67
                return MethodUsage::USAGE_GETTER;
68
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
69
70
            // setters
71
            case array('class_attribute', '=', 'var'):
72
            case array('class_attribute', '=', 'cast', 'var'):
73
            case array('class_attribute', '=', 'var', 'return', '$this'):
74
            case array('class_attribute', '=', 'cast', 'var', 'return', '$this'):
75
                return MethodUsage::USAGE_SETTER;
76
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
77
        }
78
79
80
        return MethodUsage::USAGE_UNKNWON;
81
    }
82
}