ForIn::afterScript()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * For licensing information, please see the LICENSE file accompanied with this file.
4
 *
5
 * @author Gerard van Helden <[email protected]>
6
 * @copyright 2012 Gerard van Helden <http://melp.nl>
7
 */
8
9
namespace Zicht\Tool\Script\Node\Script;
10
11
use Zicht\Tool\Script\Buffer;
12
use Zicht\Tool\Script\Node\Branch;
13
14
15
/**
16
 * Class ForIn
17
 *
18
 * @package Zicht\Tool\Script\Node\Script
19
 */
20
class ForIn extends Branch implements Annotation
21
{
22
    /**
23
     * Construct the decorator with the specified expression as the first and only child node.
24
     *
25
     * @param \Zicht\Tool\Script\Node\Node $expr
26
     * @param string $key
27
     * @param string $value
28
     */
29
    public function __construct($expr, $key, $value)
30
    {
31
        parent::__construct(array($expr));
32
        $this->key = $key ?: '_key';
0 ignored issues
show
Bug introduced by
The property key does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
        $this->value = $value ?: '_value';
0 ignored issues
show
Bug introduced by
The property value does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
    }
35
    /**
36
     * Allows the annotation to modify the buffer before the script is compiled.
37
     *
38
     * @param Buffer $buffer
39
     * @return void
40
     */
41
    public function beforeScript(Buffer $buffer)
42
    {
43
        $buffer->write('foreach ((array)');
44
        $this->nodes[0]->compile($buffer);
45
        $buffer
46
            ->raw(' as $_key => $_value) {')->eol()->indent(1)
47
            ->writeln(sprintf('$z->push(\'%s\', $_key);', $this->key))
48
            ->writeln(sprintf('$z->push(\'%s\', $_value);', $this->value))
49
        ;
50
    }
51
52
    /**
53
     * Allows the annotation to modify the buffer after the script is compiled.
54
     *
55
     * @param Buffer $buffer
56
     * @return void
57
     */
58
    public function afterScript(Buffer $buffer)
59
    {
60
        $buffer->write(sprintf('$z->pop(\'%s\');', $this->key));
61
        $buffer->write(sprintf('$z->pop(\'%s\');', $this->value));
62
        $buffer->indent(-1);
63
        $buffer->writeln('}');
64
    }
65
66
    /**
67
     * Compiles the node into the buffer.
68
     *
69
     * @param \Zicht\Tool\Script\Buffer $buffer
70
     * @return void
71
     */
72
    public function compile(Buffer $buffer)
73
    {
74
    }
75
}
76