Completed
Pull Request — master (#5)
by Cees-Jan
02:03
created

AsyncTableGenerator::extractNamespace()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3.3332
1
<?php
2
3
namespace WyriHaximus\React\Cake\Orm;
4
5
use Cake\Datasource\EntityInterface;
6
use Composer\Autoload\ClassLoader;
7
use Generator;
8
use PhpParser\BuilderFactory;
9
use PhpParser\Node;
10
use PhpParser\Parser;
11
use PhpParser\ParserFactory;
12
use PhpParser\PrettyPrinter\Standard;
13
use RuntimeException;
14
15
final class AsyncTableGenerator
16
{
17
    /**
18
     * @var string
19
     */
20
    private $storageLocation;
21
22
    /**
23
     * @var BuilderFactory
24
     */
25
    private $factory;
26
27
    /**
28
     * @var Parser
29
     */
30
    private $parser;
31
32
    /**
33
     * @var ClassLoader
34
     */
35
    private $classLoader;
36
37
    /**
38
     * @param string $storageLocation
39
     */
40 1
    public function __construct($storageLocation)
41
    {
42 1
        $this->storageLocation = $storageLocation;
43 1
        $this->factory = new BuilderFactory();
44 1
        $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
45 1
        $this->classLoader = $this->locateClassloader();
46 1
    }
47
48 1
    private function locateClassloader()
49
    {
50
        foreach ([
51 1
            dirname(__DIR__) . DS . 'vendor' . DS . 'autoload.php',
52 1
            dirname(dirname(dirname(__DIR__))) . DS . 'autoload.php',
53 1
        ] as $path) {
54 1
            if (file_exists($path)) {
55 1
                return require $path;
56
            }
57
        }
58
59
        throw new RuntimeException('Unable to locate class loader');
60
    }
61
62
    /**
63
     * @param string $tableClass
64
     * @return string
65
     */
66 1
    public function generate($tableClass)
67
    {
68 1
        $fileName = $this->classLoader->findFile($tableClass);
69 1
        $contents = file_get_contents($fileName);
70 1
        $ast = $this->parser->parse($contents);
71
72 1
        $hashedClass = 'C' . md5($tableClass) . '_F' . md5($contents);
73
74 1
        $class = $this->factory->class($hashedClass)
75 1
            ->extend('BaseTable')
76 1
            ->implement('AsyncTableInterface')
77 1
        ;
78
79 1
        $class->addStmt(
80 1
            new Node\Stmt\TraitUse([
81 1
                new Node\Name('AsyncTable')
82 1
            ])
83 1
        );
84
85 1
        $class->addStmt(
86 1
            self::createMethod(
87 1
                'save',
88
                [
89 1
                    new Node\Param('entity', null, 'EntityInterface'),
90 1
                    new Node\Param('options', new Node\Expr\Array_()),
91
                ]
92 1
            )
93 1
        );
94
95 1
        foreach ($this->extractMethods($ast) as $method) {
0 ignored issues
show
Bug introduced by
It seems like $ast defined by $this->parser->parse($contents) on line 70 can also be of type null; however, WyriHaximus\React\Cake\O...rator::extractMethods() 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...
96 1
            $class->addStmt(
97 1
                self::createMethod(
98 1
                    $method->name,
99 1
                    $method->params
100 1
                )
101 1
            );
102 1
        }
103
104 1
        $node = $this->factory->namespace('WyriHaximus\GeneratedAsyncCakeTable\\' . $this->extractNamespace($ast))
0 ignored issues
show
Bug introduced by
It seems like $ast defined by $this->parser->parse($contents) on line 70 can also be of type null; however, WyriHaximus\React\Cake\O...tor::extractNamespace() does only seem to accept array, 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...
105 1
            ->addStmt($this->factory->use(EntityInterface::class))
106 1
            ->addStmt($this->factory->use($tableClass)->as('BaseTable'))
107 1
            ->addStmt($this->factory->use(AsyncTable::class))
108 1
            ->addStmt($this->factory->use(AsyncTableInterface::class))
109 1
            ->addStmt($class)
110 1
            ->getNode()
111 1
        ;
112
113 1
        $prettyPrinter = new Standard();
114 1
        file_put_contents(
115 1
            $this->storageLocation . DIRECTORY_SEPARATOR . $hashedClass . '.php',
116 1
            $prettyPrinter->prettyPrintFile([
117
                $node
118 1
            ]) . PHP_EOL
119 1
        );
120
121 1
        return $hashedClass;
122
    }
123
124 1
    protected function createMethod($method, array $params)
125
    {
126 1
        return $this->factory->method($method)
127 1
            ->makePublic()
128 1
            ->addParams($params)
129 1
            ->addStmt(
130 1
                new Node\Stmt\Return_(
131 1
                    new Node\Expr\MethodCall(
132 1
                        new Node\Expr\Variable('this'),
133 1
                        'callAsyncOrSync',
134
                        [
0 ignored issues
show
Documentation introduced by
array(new \PhpParser\Nod...hodArguments($params))) is of type array<integer,object<Php...\Node\\Expr\\Array_>"}>, but the function expects a array<integer,object<PhpParser\Node\Arg>>.

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...
135 1
                            new Node\Scalar\String_($method),
136 1
                            new Node\Expr\Array_(
137 1
                                $this->createMethodArguments($params)
138 1
                            ),
139
                        ]
140 1
                    )
141 1
                )
142 1
            )
143 1
        ;
144
    }
145
146
    /**
147
     * @param array $params
148
     * @return array
149
     */
150 1
    protected function createMethodArguments(array $params)
151
    {
152 1
        $arguments = [];
153 1
        foreach ($params as $param) {
154 1
            if (!($param instanceof Node\Param)) {
155
                continue;
156
            }
157 1
            $arguments[] = new Node\Expr\Variable($param->name);
158 1
        }
159 1
        return $arguments;
160
    }
161
162
    /**
163
     * @param Node[] $ast
164
     * @return Generator
165
     */
166 1 View Code Duplication
    protected function extractMethods(array $ast)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168 1
        foreach ($ast as $node) {
169 1
            if (!isset($node->stmts)) {
0 ignored issues
show
Bug introduced by
Accessing stmts on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
170
                continue;
171
            }
172
173 1
            foreach ($this->iterageStmts($node->stmts) as $stmt) {
0 ignored issues
show
Bug introduced by
Accessing stmts on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
174 1
                yield $stmt;
175 1
            }
176 1
        }
177 1
    }
178
179 1 View Code Duplication
    protected function iterageStmts(array $stmts)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
180
    {
181 1
        foreach ($stmts as $stmt) {
182 1
            if ($stmt instanceof Node\Stmt\ClassMethod) {
183 1
                yield $stmt;
184 1
            }
185
186 1
            if (!isset($stmt->stmts)) {
187 1
                continue;
188
            }
189
190 1
            foreach ($this->iterageStmts($stmt->stmts) as $stmt) {
191 1
                yield $stmt;
192 1
            }
193 1
        }
194 1
    }
195
196 1
    protected function extractNamespace(array $ast)
197
    {
198 1
        foreach ($ast as $node) {
199 1
            if ($node instanceof Node\Stmt\Namespace_) {
200 1
                return (string)$node->name;
201
            }
202
        }
203
204
        return 'N' . uniqid('', true);
205
    }
206
}
207