|
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()); |
|
|
|
|
|
|
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())); |
|
|
|
|
|
|
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
|
|
|
|
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:
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: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: