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

GenerateShell::all()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
ccs 0
cts 15
cp 0
rs 8.8571
c 1
b 0
f 0
cc 6
eloc 8
nc 12
nop 0
crap 42
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\Plugin;
10
use Cake\ORM\TableRegistry;
11
use WyriHaximus\React\Cake\Orm\AsyncTableRegistry;
12
13
class GenerateShell extends Shell
14
{
15
    public function all()
16
    {
17
        foreach (App::path('Table') as $path) {
18
            if (is_dir($path)) {
19
                $this->iteratePath($path);
20
            }
21
        }
22
23
        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...
24
            foreach (App::path('Table', $plugin) as $path) {
25
                if (is_dir($path)) {
26
                    $this->iteratePath($path);
27
                }
28
            }
29
        }
30
31
    }
32
33
    public function iteratePath($path)
34
    {
35
        foreach ($this->setupIterator($path) as $item) {
36
            $this->iterateClasses($this->getClassByFile($item));
37
        }
38
    }
39
40
    public function iterateClasses($classes)
41
    {
42
        foreach ($classes as $class) {
43
            AsyncTableRegistry::get(
44
                TableRegistry::get(
45
                    md5($class),
46
                    [
47
                        'className' => $class,
48
                    ]
49
                )
50
            );
51
        }
52
    }
53
54
    public function getClassByFile($fileName)
55
    {
56
        return (new ClassReflector(new SingleFileSourceLocator($fileName)))->getAllClasses();
57
    }
58
59
    protected function setupIterator($path)
60
    {
61
        return new \RegexIterator(new \RecursiveIteratorIterator(
62
            new \RecursiveDirectoryIterator(
63
                $path,
64
                \FilesystemIterator::KEY_AS_PATHNAME |
65
                \FilesystemIterator::CURRENT_AS_FILEINFO |
66
                \FilesystemIterator::SKIP_DOTS
67
            ),
68
            \RecursiveIteratorIterator::CHILD_FIRST,
69
            \RecursiveIteratorIterator::CATCH_GET_CHILD
70
        ), '/.*?.php$/', \RegexIterator::GET_MATCH);
71
    }
72
73
74
    /**
75
     * Set options for this console.
76
     *
77
     * @return \Cake\Console\ConsoleOptionParser
78
     */
79
    // @codingStandardsIgnoreStart
80
    public function getOptionParser()
81
    {
82
        // @codingStandardsIgnoreEnd
83
        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 83 which is incompatible with the return type of the parent method Cake\Console\Shell::getOptionParser of type Cake\Console\ConsoleOptionParser.
Loading history...
84
            'all',
85
            [
86
                'short' => 'a',
87
                // @codingStandardsIgnoreStart
88
                'help' => __('Searches and pregenerates all async tables it finds.'),
89
                // @codingStandardsIgnoreEnd
90
            ]
91
        // @codingStandardsIgnoreStart
92
        )->description(__('Async table pregenerator'));
93
        // @codingStandardsIgnoreEnd
94
    }
95
}
96