Completed
Push — master ( 0b755b...d8f6c8 )
by Дмитрий
07:19
created

RuntimeClassDefintionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 55
rs 10
c 1
b 0
f 1
wmc 8
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstruct() 0 5 1
A testHasMethod() 0 11 2
A testHasConst() 0 11 2
A testHasConstWithParent() 0 8 1
A testHasProperty() 0 11 2
1
<?php
2
3
namespace Tests\PHPSA\Defintion;
4
5
use PHPSA\Compiler\Parameter;
6
use PHPSA\Definition\RuntimeClassDefinition;
7
use PHPSA\Variable;
8
use ReflectionClass;
9
use Tests\PHPSA\TestCase;
10
11
class RuntimeClassDefintionTest extends TestCase
12
{
13
    const TEST_CLASS_NAME = 'PHPSA\Definition\ClassDefinition';
14
15
    public function testConstruct()
16
    {
17
        $definition = new RuntimeClassDefinition(new ReflectionClass(self::TEST_CLASS_NAME));
18
        $definition->compile($this->getContext());
0 ignored issues
show
Unused Code introduced by
The call to the method PHPSA\Definition\RuntimeClassDefinition::compile() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
19
    }
20
21
    public function testHasMethod()
22
    {
23
        $reflection = new ReflectionClass(self::TEST_CLASS_NAME);
24
        $definition = new RuntimeClassDefinition($reflection);
25
26
        foreach ($reflection->getMethods() as $method) {
27
            static::assertTrue($definition->hasMethod($method->getName()));
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
28
        }
29
30
        static::assertFalse($definition->hasMethod('XXXXXXXXXXXXXX'));
31
    }
32
33
    public function testHasConst()
34
    {
35
        $reflection = new ReflectionClass('PHPSA\Context');
36
        $definition = new RuntimeClassDefinition($reflection);
37
38
        foreach ($reflection->getConstants() as $constant) {
39
            static::assertTrue($definition->hasConst($constant));
40
        }
41
42
        static::assertFalse($definition->hasConst('XXXXXXXXX'));
43
    }
44
45
    public function testHasConstWithParent()
46
    {
47
        $reflection = new ReflectionClass(Parameter::class);
48
        $definition = new RuntimeClassDefinition($reflection);
49
50
        static::assertFalse($definition->hasConst('BRANCH_ROOT'));
51
        static::assertTrue($definition->hasConst('BRANCH_ROOT', true));
52
    }
53
54
    public function testHasProperty()
55
    {
56
        $reflection = new ReflectionClass('PHPSA\Context');
57
        $definition = new RuntimeClassDefinition($reflection);
58
59
        foreach ($reflection->getProperties() as $property) {
60
            static::assertTrue($definition->hasProperty($property->getName()));
61
        }
62
63
        static::assertFalse($definition->hasProperty('XXXXX'));
64
    }
65
}
66