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

Converter::implementExist()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 10
cp 0.9
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 6
nop 3
crap 5.025
1
<?php
2
3
namespace PhpToZephir\Converter;
4
5
use PhpToZephir\Logger;
6
use PhpToZephir\NodeFetcher;
7
use PhpParser\Node\Expr;
8
use PhpParser\Node\Stmt;
9
use PhpToZephir\Converter\Printer\Expr\ClosurePrinter;
10
use PhpToZephir\ClassCollector;
11
12
class Converter
13
{
14
    /**
15
     * @var Dispatcher
16
     */
17
    private $dispatcher = null;
18
    /**
19
     * @var NodeFetcher
20
     */
21
    private $nodeFetcher = null;
22
23
    /**
24
     * @param Dispatcher  $dispatcher
25
     * @param NodeFetcher $nodeFetcher
26
     */
27 1
    public function __construct(Dispatcher $dispatcher, NodeFetcher $nodeFetcher)
28
    {
29 1
        $this->dispatcher = $dispatcher;
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...
30 1
        $this->nodeFetcher = $nodeFetcher;
31 1
    }
32
33
    /**
34
     * @param array          $stmts
35
     * @param ClassCollector $classCollector
36
     * @param Logger         $logger
37
     * @param string         $fileName
38
     * @param array          $classCollected
39
     *
40
     * @return array
41
     */
42 82
    public function nodeToZephir(array $stmts, ClassCollector $classCollector, Logger $logger, $fileName = null, array $classCollected = array())
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 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...
43
    {
44 82
        $classInformation = ClassInformationFactory::getInstance();
45 82
        $metadata = $classInformation->getClassesMetdata($stmts);
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...
46
47 82
        $this->implementsExist($metadata, $classCollector);
48
49
        return array(
50 81
            'code' => $this->dispatcher->convert($stmts, $metadata, $classCollector, $logger),
51 81
            'namespace' => $metadata->getNamespace(),
52 81
            'additionalClass' => $this->findAdditionalClasses($stmts, $logger),
53 81
        );
54
    }
55
56
    /**
57
     * @param ClassMetadata $metadata
58
     * @param ClassCollector $classCollector
59
     */
60 82
    private function implementsExist(ClassMetadata $metadata, ClassCollector $classCollector)
61
    {
62 82
        foreach ($metadata->getImplements() as $implements) {
63 5
            $this->implementExist($metadata, $classCollector, $implements);
64 81
        }
65 81
    }
66
67
    /**
68
     * @param ClassMetadata $metadata
69
     * @param ClassCollector $classCollector
70
     * @param string $implements
71
     * @throws \Exception
72
     * @return boolean
73
     */
74 5
    private function implementExist(ClassMetadata $metadata, ClassCollector $classCollector, $implements)
75
    {
76
        // Class is in actual namespace
77 5
        if (array_key_exists($metadata->getNamespace() . '\\' . $implements, $classCollector->getCollected())) {
78 3
            return true;
79
        }
80
81 2
        foreach ($metadata->getClasses() as $use) {
82 1
            if (substr(strrchr($use, "\\"), 1) === $implements) {
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...
83 1
                return true;
84
            }
85 2
        }
86
87 1
        if (interface_exists($implements)) {
88
            return true;
89
        }
90
91 1
        throw new \Exception(sprintf('interface %s does not exist', $implements));
92
    }
93
94
    /**
95
     * @param array $stmts
96
     *
97
     * @return array
98
     */
99 81
    private function findAdditionalClasses(array $stmts, Logger $logger)
100
    {
101 81
        $closurePrinter = new ClosurePrinter($this->dispatcher, $logger);
102 81
        $lastMethod = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
103 81
        $aditionalClass = array();
104 81
        $number = 0;
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...
105
106 81
        foreach ($this->nodeFetcher->foreachNodes($stmts) as $nodeData) {
107 81
            $node = $nodeData['node'];
108 81
            if ($node instanceof Stmt\ClassMethod) {
109 79
                $lastMethod = $node->name;
110 81
            } elseif ($node instanceof Expr\Closure) {
111 1
                $aditionalClass[] = $closurePrinter->createClosureClass($node, $lastMethod, $number);
112 1
                ++$number;
113 1
            }
114 81
        }
115
116 81
        return $aditionalClass;
117
    }
118
}
119