Completed
Pull Request — master (#448)
by
unknown
08:31
created

RoleOfMethodDetectorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testICanDetectRoleOfMethod() 0 19 5
A provideExamples() 0 23 2
1
<?php
2
namespace Test\Hal\Metric\Helper;
3
4
use Hal\Metric\Helper\RoleOfMethodDetector;
5
use PhpParser\Node\Stmt\Class_;
6
use PhpParser\Node\Stmt\ClassMethod;
7
use PhpParser\ParserFactory;
8
9
/**
10
 * @group method
11
 * @group helper
12
 * @group parsing
13
 */
14
class RoleOfMethodDetectorTest extends \PHPUnit\Framework\TestCase
15
{
16
    /**
17
     * @dataProvider provideExamples
18
     *
19
     * @param string $expected
20
     * @param string $code
21
     */
22
    public function testICanDetectRoleOfMethod($expected, $code)
23
    {
24
        $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
25
        $stmt = $parser->parse($code);
26
        $this->assertNotNull($stmt);
27
28
        $helper = new RoleOfMethodDetector();
29
30
        foreach ($stmt as $node) {
0 ignored issues
show
Bug introduced by
The expression $stmt of type array<integer,object<PhpParser\Node\Stmt>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
31
            if ($node instanceof Class_) {
32
                foreach ($node->stmts as $sub) {
33
                    if ($sub instanceof ClassMethod) {
34
                        $type = $helper->detects($sub);
35
                        $this->assertEquals($expected, $type);
36
                    }
37
                }
38
            }
39
        }
40
    }
41
42
    /** @return mixed[] */
43
    public function provideExamples()
44
    {
45
        $examples = [
46
            'getter' => ['getter', '<?php class A { function getName(){ return $this->name; } }  ?>'],
47
            'getter with string cast' => ['getter', '<?php class A { function getName(){ return (string) $this->name; } }  ?>'],
48
            'getter with int cast' => ['getter', '<?php class A { function getName(){ return (int) $this->name; } }  ?>'],
49
            'setter' => ['setter', '<?php class A { function setName($string){ $this->name = $name; } } ?>'],
50
            'setter with string cast' => ['setter', '<?php class A { function setName($string){ $this->name = (string) $name; } } ?>'],
51
            'setter with $this return' => ['setter', '<?php class A { function setName($string){ $this->name = (string) $name; return $this; } } ?>'],
52
            'neither setter nor getter' => [null, '<?php class A { function foo($string){ $this->name = (string) $name * 3; } } ?>'],
53
        ];
54
        if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
55
            $examples['getter with return scalar'] = ['getter', '<?php class A { function getName(): string { return $this->name; } }'];
56
            $examples['setter with scalar hint and return void'] = ['setter', '<?php class A { function setName(string $name): void { $this->name = $name; } }'];
57
            $examples['getter with return object'] = ['getter', '<?php class A { function getName(): Name { return $this->name; } }'];
58
            $examples['setter with object hint and return void'] = ['setter', '<?php class A { function setName(Name $name): void { $this->name = $name; } }'];
59
            $examples['getter with return optional'] = ['getter', '<?php class A { function isOk(): ?bool { return $this->isOk; } }'];
60
            $examples['setter fluent with param optional'] = ['setter', '<?php class A { function setOk(?bool $ok): self { $this->isOk = $ok; return $this; } }'];
61
            $examples['setter fluent non typed with param optional'] = ['setter', '<?php class A { function setOk(?bool $ok) { $this->isOk = $ok; return $this; } }'];
62
            $examples['setter with param optional'] = ['setter', '<?php class A { function setOk(?bool $ok) { $this->isOk = $ok; } }'];
63
        }
64
        return $examples;
65
    }
66
}
67