Completed
Pull Request — master (#87)
by Kévin
03:21
created

ContextTest::testVariableVariablesAreIgnored()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 9.4285
1
<?php
2
3
namespace Tests\PHPSA;
4
5
use PHPSA\CompiledExpression;
6
use PHPSA\Variable;
7
8
class ContextTest extends TestCase
9
{
10
    public function testModifyReferencedVariables()
11
    {
12
        $context = $this->getContext();
13
14
        /**
15
         * This variable is not needed for change
16
         */
17
        $variableAValue = 1;
18
        $variableAType = CompiledExpression::INTEGER;
19
        $context->addVariable(
20
            $variableA = new Variable('a', $variableAValue, $variableAType)
21
        );
22
23
        /**
24
         * $b = true;
25
         */
26
        $context->addVariable(
27
            $variableB = new Variable('b', null, CompiledExpression::BOOLEAN, true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
28
        );
29
30
        /**
31
         * $c = &$b;
32
         */
33
        $variableC = new Variable('c');
34
        $variableC->setReferencedTo($variableB);
35
36
        $context->addVariable(
37
            $variableC
38
        );
39
40
        $newType = CompiledExpression::INTEGER;
41
        $newValue = 55;
42
43
        /**
44
         * $b = {$newValue};
45
         * After it variable $c will change type and value
46
         */
47
        $context->modifyReferencedVariables($variableB, $newType, $newValue);
48
49
        self::assertSame($newValue, $variableC->getValue());
50
        self::assertSame($newType, $variableC->getType());
51
52
        /**
53
         * Assert that variable $a was not changed
54
         */
55
        self::assertSame($variableAValue, $variableA->getValue());
56
        self::assertSame($variableAType, $variableA->getType());
57
    }
58
59
60
    public function testVariableVariablesAreIgnored()
61
    {
62
        $context = $this->getContext();
63
64
        $variableAName = new Variable('aName', 'a', CompiledExpression::STRING);
65
        $context->addVariable(
66
            $variableA = new Variable($variableAName, 1, CompiledExpression::INTEGER)
0 ignored issues
show
Documentation introduced by
$variableAName is of type object<PHPSA\Variable>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
        );
68
69
        self::assertFalse($context->addVariable($variableA));
70
        self::assertNull($context->getSymbol($variableAName));
71
    }
72
}
73