Completed
Pull Request — master (#5)
by Cees-Jan
09:17
created

GenerateShell::getClassByFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace WyriHaximus\React\Cake\Orm\Shell;
4
5
use BetterReflection\Reflector\ClassReflector;
6
use BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
7
use Cake\Console\Shell;
8
use Cake\Core\App;
9
use Cake\Core\Configure;
10
use Cake\Core\Plugin;
11
use Cake\ORM\TableRegistry;
12
use WyriHaximus\React\Cake\Orm\AsyncTableGenerator;
13
use WyriHaximus\React\Cake\Orm\AsyncTableRegistry;
14
15
class GenerateShell extends Shell
16
{
17
    public function all()
18
    {
19
        foreach (App::path('Model/Table') as $path) {
20
            if (is_dir($path)) {
21
                $this->iteratePath($path);
22
            }
23
        }
24
25
        foreach (Plugin::loaded() as $plugin) {
0 ignored issues
show
Bug introduced by
The expression \Cake\Core\Plugin::loaded() of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
26
            foreach (App::path('Model/Table', $plugin) as $path) {
27
                if (is_dir($path)) {
28
                    $this->iteratePath($path);
29
                }
30
            }
31
        }
32
33
    }
34
35
    public function iteratePath($path)
36
    {
37
        foreach ($this->setupIterator($path) as $item) {
38
            $this->iterateClasses($this->getClassByFile(current($item)));
39
        }
40
    }
41
42
    public function iterateClasses($classes)
43
    {
44
        foreach ($classes as $class) {
45
            $className = $class->getName();
46
            (new AsyncTableGenerator(Configure::read('WyriHaximus.React.Cake.Orm.Cache.AsyncTables')))->generate($className, true);
47
        }
48
    }
49
50
    public function getClassByFile($fileName)
51
    {
52
        return (new ClassReflector(new SingleFileSourceLocator($fileName)))->getAllClasses();
53
    }
54
55
    protected function setupIterator($path)
56
    {
57
        return new \RegexIterator(new \RecursiveIteratorIterator(
58
            new \RecursiveDirectoryIterator(
59
                $path,
60
                \FilesystemIterator::KEY_AS_PATHNAME |
61
                \FilesystemIterator::CURRENT_AS_FILEINFO |
62
                \FilesystemIterator::SKIP_DOTS
63
            ),
64
            \RecursiveIteratorIterator::CHILD_FIRST,
65
            \RecursiveIteratorIterator::CATCH_GET_CHILD
66
        ), '/.*?.php$/', \RegexIterator::GET_MATCH);
67
    }
68
69
70
    /**
71
     * Set options for this console.
72
     *
73
     * @return \Cake\Console\ConsoleOptionParser
74
     */
75
    // @codingStandardsIgnoreStart
76
    public function getOptionParser()
77
    {
78
        // @codingStandardsIgnoreEnd
79
        return parent::getOptionParser()->addSubcommand(
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::getOptionParser(... table pregenerator')); of type Cake\Console\ConsoleOptionParser|string adds the type string to the return on line 79 which is incompatible with the return type of the parent method Cake\Console\Shell::getOptionParser of type Cake\Console\ConsoleOptionParser.
Loading history...
80
            'all',
81
            [
82
                'short' => 'a',
83
                // @codingStandardsIgnoreStart
84
                'help' => __('Searches and pregenerates all async tables it finds.'),
85
                // @codingStandardsIgnoreEnd
86
            ]
87
        // @codingStandardsIgnoreStart
88
        )->description(__('Async table pregenerator'));
89
        // @codingStandardsIgnoreEnd
90
    }
91
}
92