Completed
Pull Request — master (#5)
by Cees-Jan
11:35 queued 08:19
created

GenerateShell::getOptionParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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(
47
                Configure::read('WyriHaximus.React.Cake.Orm.Cache.AsyncTables')
48
            ))->generate($className, true);
49
        }
50
    }
51
52
    public function getClassByFile($fileName)
53
    {
54
        return (new ClassReflector(new SingleFileSourceLocator($fileName)))->getAllClasses();
55
    }
56
57
    protected function setupIterator($path)
58
    {
59
        return new \RegexIterator(new \RecursiveIteratorIterator(
60
            new \RecursiveDirectoryIterator(
61
                $path,
62
                \FilesystemIterator::KEY_AS_PATHNAME |
63
                \FilesystemIterator::CURRENT_AS_FILEINFO |
64
                \FilesystemIterator::SKIP_DOTS
65
            ),
66
            \RecursiveIteratorIterator::CHILD_FIRST,
67
            \RecursiveIteratorIterator::CATCH_GET_CHILD
68
        ), '/.*?.php$/', \RegexIterator::GET_MATCH);
69
    }
70
71
72
    /**
73
     * Set options for this console.
74
     *
75
     * @return \Cake\Console\ConsoleOptionParser
76
     */
77
    // @codingStandardsIgnoreStart
78
    public function getOptionParser()
79
    {
80
        // @codingStandardsIgnoreEnd
81
        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 81 which is incompatible with the return type of the parent method Cake\Console\Shell::getOptionParser of type Cake\Console\ConsoleOptionParser.
Loading history...
82
            'all',
83
            [
84
                'short' => 'a',
85
                // @codingStandardsIgnoreStart
86
                'help' => __('Searches and pregenerates all async tables it finds.'),
87
                // @codingStandardsIgnoreEnd
88
            ]
89
        // @codingStandardsIgnoreStart
90
        )->description(__('Async table pregenerator'));
91
        // @codingStandardsIgnoreEnd
92
    }
93
}
94