Completed
Pull Request — master (#35)
by Saif Eddin
02:55 queued 38s
created

ArrayDimFetchPrinter::splitArray()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6.0029

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 22
cts 23
cp 0.9565
rs 8.439
c 0
b 0
f 0
cc 6
eloc 23
nc 12
nop 2
crap 6.0029
1
<?php
2
3
namespace PhpToZephir\Converter\Printer\Expr;
4
5
use PhpToZephir\Converter\Dispatcher;
6
use PhpToZephir\Logger;
7
use PhpParser\Node\Expr;
8
use PhpToZephir\Converter\Manipulator\ArrayManipulator;
9
10
class ArrayDimFetchPrinter
11
{
12
    /**
13
     * @var Dispatcher
14
     */
15
    private $dispatcher = null;
16
    /**
17
     * @var Logger
18
     */
19
    private $logger = null;
20
    /**
21
     * @var ArrayManipulator
22
     */
23
    private $arrayManipulator = null;
24
    /**
25
     * @var array
26
     */
27
    private static $createdVars = array();
28
29
    /**
30
     * @param Dispatcher       $dispatcher
31
     * @param Logger           $logger
32
     * @param ArrayManipulator $arrayManipulator
33
     */
34 1
    public function __construct(Dispatcher $dispatcher, Logger $logger, ArrayManipulator $arrayManipulator)
35
    {
36 1
        $this->dispatcher = $dispatcher;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
37 1
        $this->logger = $logger;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
38 1
        $this->arrayManipulator = $arrayManipulator;
39 1
    }
40
    
41 79
    public static function resetCreatedVars()
42
    {
43 79
        self::$createdVars = array();
44 79
    }
45
46 1
    public static function getType()
47
    {
48 1
        return 'pExpr_ArrayDimFetch';
49
    }
50
51 20
    public function convert(Expr\ArrayDimFetch $node, $returnAsArray = false)
52
    {
53 20
        $collected = $this->arrayManipulator->arrayNeedToBeSplit($node);
54
55 20
        if ($collected !== false) {
56 5
            return $this->splitArray($collected, $returnAsArray);
57
        }
58
59 15
        $result = $this->dispatcher->pVarOrNewExpr($node->var)
60 15
             .'['.(null !== $node->dim ? $this->dispatcher->p($node->dim) : '').']';
61
62 15
        if ($returnAsArray === true) {
63
            return array(
64 15
                'head' => '',
65 15
                'lastExpr' => $result,
66 15
                'vars' => array()
67 15
            );
68
        }
69
70 15
        return $result;
71
    }
72
73
    /**
74
     * @param bool $returnAsArray
75
     */
76 5
    private function splitArray(array $collected, $returnAsArray)
77
    {
78 5
        $var         = $collected[0];
79 5
        $lastExpr    = $this->dispatcher->p($var);
80 5
        $createAsTmp = array();
81 5
        $head        = array();
82 5
        $vars        = array();
83
84 5
        unset($collected[0]);
85
86 5
        foreach ($collected as $expr) {
87 5
            if ($expr['splitTab'] === true) {
88 5
                $createAsTmp = $this->addAsTmp($createAsTmp, $expr['var'] );
89 5
                $tmpVarName  = 'tmp' . ucfirst($expr['var']) . $createAsTmp[$expr['var']];
90 5
                $vars[]      = $tmpVarName;
91 5
                $head[]      = $expr['expr'];
92 5
                $head[]      = 'let ' . $tmpVarName . ' = ' . $expr['var'] . ";\n";
93 5
                $lastExpr .= '[' . $tmpVarName .']';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
94 5
            } else {
95 2
                $lastExpr .= '['.$expr['expr'].']';
96
            }
97 5
        }
98
99 5
        if ($returnAsArray === true) {
100
            return array(
101 5
                'head' => !empty($head) ? "\n" . implode("", $head) . "\n" : "",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
102 5
                'lastExpr' => $lastExpr,
103
                'vars' => $vars
104 5
            );
105
        }
106
107
        return (!empty($head) ? "\n" . implode("", $head) . "\n" : "").$lastExpr;
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
108
    }
109
    
110 5
    private function addAsTmp(array $createAsTmp, $varname)
111
    {
112 5
        if (!isset(self::$createdVars[$this->dispatcher->getLastMethod()])) {
113 5
            self::$createdVars[$this->dispatcher->getLastMethod()] = array();
114 5
        }
115
116 5
        if (isset(self::$createdVars[$this->dispatcher->getLastMethod()][$varname])) {
117 3
            self::$createdVars[$this->dispatcher->getLastMethod()][$varname]++;
118 3
            $createAsTmp[$varname] = self::$createdVars[$this->dispatcher->getLastMethod()][$varname];
119 3
        } else {
120 5
            self::$createdVars[$this->dispatcher->getLastMethod()][$varname] = 1;
121 5
            $createAsTmp[$varname] = 1;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 43 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
122
        }
123
124 5
        return $createAsTmp;
125
    }
126
}
127