Completed
Push — master ( 4643e1...064b7a )
by Дмитрий
03:17
created

GetParametersCheck   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 86.11%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 31
cts 36
cp 0.8611
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
C pass() 0 35 7
A getConfiguration() 0 9 1
A getRegister() 0 6 1
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Statement\MagicMethod;
7
8
use PhpParser\Node\Stmt\ClassMethod;
9
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
10
use PHPSA\Analyzer\Pass\ConfigurablePassInterface;
11
use PHPSA\Check;
12
use PHPSA\Context;
13
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
14
15
class GetParametersCheck implements ConfigurablePassInterface, AnalyzerPassInterface
16
{
17
    /**
18
     * @param ClassMethod $methodStmt
19
     * @param Context $context
20
     * @return bool
21
     */
22 20
    public function pass(ClassMethod $methodStmt, Context $context)
23
    {
24 20
        if ($methodStmt->name == '__get') {
25 1
            if (count($methodStmt->params) == 0) {
26 1
                $context->notice(
27 1
                    'magic.get.wrong-parameters',
28 1
                    'Magic method __get must take 1 parameter at least',
29 1
                    $methodStmt,
30
                    Check::CHECK_SAFE
31 1
                );
32 1
            }
33 1
        }
34
35 20
        if ($methodStmt->name == '__set') {
36 1
            if (count($methodStmt->params) < 2) {
37 1
                $context->notice(
38 1
                    'magic.get.wrong-parameters',
39 1
                    'Magic method __set must take 2 parameters at least',
40 1
                    $methodStmt,
41
                    Check::CHECK_SAFE
42 1
                );
43 1
            }
44 1
        }
45
46 20
        if ($methodStmt->name == '__clone') {
47 1
            if (count($methodStmt->params) > 0) {
48 1
                $context->notice(
49 1
                    'magic.get.wrong-parameters',
50 1
                    'Magic method __clone cannot accept arguments',
51 1
                    $methodStmt,
52
                    Check::CHECK_SAFE
53 1
                );
54 1
            }
55 1
        }
56 20
    }
57
58
    /**
59
     * @return TreeBuilder
60
     */
61
    public function getConfiguration()
62
    {
63
        $treeBuilder = new TreeBuilder();
64
        $treeBuilder->root('method_cannot_return')
65
            ->canBeDisabled()
66
        ;
67
68
        return $treeBuilder;
69
    }
70
71
    /**
72
     * @return array
73
     */
74 1
    public function getRegister()
75
    {
76
        return [
77
            \PhpParser\Node\Stmt\ClassMethod::class
78 1
        ];
79
    }
80
}
81