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

Engine::convert()   C

Complexity

Conditions 8
Paths 40

Size

Total Lines 91
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 55
CRAP Score 8.3676

Importance

Changes 0
Metric Value
dl 0
loc 91
ccs 55
cts 67
cp 0.8209
rs 5.574
c 0
b 0
f 0
cc 8
eloc 61
nc 40
nop 3
crap 8.3676

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpToZephir;
4
5
use PhpParser\Parser;
6
use PhpToZephir\Converter\Converter;
7
use PhpToZephir\CodeCollector\CodeCollectorInterface;
8
9
class Engine
10
{
11
    /**
12
     * @var Parser
13
     */
14
    private $parser = null;
15
    /**
16
     * @var Converter
17
     */
18
    private $converter = null;
19
    /**
20
     * @var ClassCollector
21
     */
22
    private $classCollector = null;
23
24
    /**
25
     * @param Parser              $parser
26
     * @param Converter\Converter $converter
27
     * @param ClassCollector      $classCollector
28
     * @param ConfigWriter        $configWriter
29
     */
30 1
    public function __construct(
31
        Parser $parser,
32
        Converter $converter,
33
        ClassCollector $classCollector
34
    ) {
35 1
        $this->parser = $parser;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 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...
36 1
        $this->converter = $converter;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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->classCollector = $classCollector;
38 1
    }
39
40
    /**
41
     * @param CodeCollectorInterface $codeCollector
42
     * @param Logger                 $logger
43
     * @param string                 $filterFileName
44
     *
45
     * @return array
46
     */
47 82
    public function convert(CodeCollectorInterface $codeCollector, Logger $logger, $filterFileName = null)
48
    {
49 82
        $zephirCode = array();
50 82
        $classes = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
51
52 82
        $files = $codeCollector->getCode();
53 82
        $count = count($files);
54 82
        $codes = array();
55
56 82
        $logger->log('Collect class names');
57 82
        $progress = $logger->progress($count);
58
59 82
        foreach ($files as $fileName => $fileContent) {
60
            try {
61 82
                $codes[$fileName] = $this->parser->parse($fileContent);
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...
62 82
                $classes[$fileName] = $this->classCollector->collect($codes[$fileName], $fileName);
0 ignored issues
show
Bug introduced by
It seems like $codes[$fileName] can also be of type null; however, PhpToZephir\ClassCollector::collect() does only seem to accept array<integer,object<PhpParser\Node>>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Documentation introduced by
$fileName is of type integer|string, but the function expects a object<PhpToZephir\unknown>.

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...
63 82
            } catch (\Exception $e) {
64
                $logger->log(
65
                    sprintf(
66
                        '<error>Could not convert file'."\n".'"%s"'."\n".'cause : %s %s %s</error>'."\n",
67
                        $fileName,
68
                        $e->getMessage(),
69
                        $e->getFile(),
70
                        $e->getLine()
71
                    )
72
                );
73
            }
74 82
            $progress->advance();
75 82
        }
76
77 82
        $progress->finish();
78
79 82
        $logger->log("\nConvert php to zephir");
80 82
        $progress = $logger->progress(count($classes));
81
82 82
        foreach ($classes as $phpFile => $class) {
83 82
            if ($filterFileName !== null) {
84
                if (basename($phpFile, '.php') !== $filterFileName) {
85
                    continue;
86
                }
87
            }
88
89 82
            $phpCode = $codes[$phpFile];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
90 82
            $fileName = basename($phpFile, '.php');
91
            try {
92 82
                $converted = $this->convertCode($phpCode, $this->classCollector, $logger, $phpFile, $classes);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
93 81
                $converted['class'] = $class;
94 82
            } catch (\Exception $e) {
95 1
                $logger->log(
96 1
                    sprintf(
97 1
                        'Could not convert file "%s" cause : %s %s %s'."\n",
98 1
                        $phpFile,
99 1
                        $e->getMessage(),
100 1
                        $e->getFile(),
101 1
                        $e->getLine()
102 1
                    )
103 1
                );
104 1
                $progress->advance();
105 1
                continue;
106
            }
107
108 81
            $zephirCode[$phpFile] = array_merge(
109 81
                $converted,
110
                array(
111 81
                    'phpPath' => substr($phpFile, 0, strrpos($phpFile, '/')),
112 81
                    'fileName' => $fileName,
113 81
                    'fileDestination' => $converted['class'].'.zep',
114
                )
115 81
             );
116
117 81
            $zephirCode[$phpFile]['fileDestination'] = str_replace('\\', '/', $zephirCode[$phpFile]['fileDestination']);
118
119 81
            foreach ($converted['additionalClass'] as $aditionalClass) {
120 1
                $zephirCode[$phpFile.$aditionalClass['name']] = array_merge(
121
                    array(
122 1
                        'fileName' => $aditionalClass['name'],
123 1
                        'zephir' => $aditionalClass['code'],
124 1
                        'fileDestination' => str_replace('\\', '/', $converted['namespace']).'/'.$aditionalClass['name'].'.zep',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
125 1
                        'destination' => str_replace('\\', '/', $converted['namespace']).'/',
126
                    )
127 1
                );
128 81
            }
129
130 81
            $progress->advance();
131 82
        }
132
133 82
        $progress->finish();
134 82
        $logger->log("\n");
135
136 82
        return $zephirCode;
137
    }
138
139
    /**
140
     * @param string $phpCode
141
     *
142
     * @return string
143
     */
144 82
    private function convertCode($phpCode, ClassCollector $classCollector, Logger $logger, $fileName = null, array $classes = array())
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
145
    {
146 82
        $converted = $this->converter->nodeToZephir($phpCode, $classCollector, $logger, $fileName, $classes);
0 ignored issues
show
Documentation introduced by
$phpCode is of type string, but the function expects a array.

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...
147
148
        return array(
149 81
            'zephir' => $converted['code'],
150 81
            'php' => $phpCode,
151 81
            'namespace' => $converted['namespace'],
152 81
            'destination' => str_replace('\\', '/', $converted['namespace']).'/',
153 81
            'additionalClass' => $converted['additionalClass'],
154 81
        );
155
    }
156
}
157