GenerateShell::setupIterator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 0
cts 8
cp 0
rs 9.8333
cc 1
nc 1
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Cake\Orm\Shell;
4
5
use Cake\Console\Shell;
6
use Cake\Core\App;
7
use Cake\Core\Configure;
8
use Cake\Core\Plugin;
9
use Roave\BetterReflection\BetterReflection;
10
use Roave\BetterReflection\Reflector\ClassReflector;
11
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
12
use WyriHaximus\React\Cake\Orm\AsyncTableGenerator;
13
14
class GenerateShell extends Shell
15
{
16
    public function all()
17
    {
18
        foreach (App::path('Model/Table') as $path) {
19
            if (is_dir($path)) {
20
                $this->iteratePath($path);
21
            }
22
        }
23
24
        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...
25
            foreach (App::path('Model/Table', $plugin) as $path) {
26
                if (is_dir($path)) {
27
                    $this->iteratePath($path);
28
                }
29
            }
30
        }
31
    }
32
33
    public function iteratePath($path)
34
    {
35
        foreach ($this->setupIterator($path) as $item) {
36
            $this->iterateClasses($this->getClassByFile(current($item)));
37
        }
38
    }
39
40
    public function iterateClasses($classes)
41
    {
42
        foreach ($classes as $class) {
43
            $className = $class->getName();
44
            (new AsyncTableGenerator(
45
                Configure::read('WyriHaximus.React.Cake.Orm.Cache.AsyncTables')
46
            ))->generate($className, true);
47
        }
48
    }
49
50
    public function getClassByFile($fileName)
51
    {
52
        return (new ClassReflector(new SingleFileSourceLocator($fileName, (new BetterReflection())->astLocator())))->getAllClasses();
53
    }
54
55
    /**
56
     * Set options for this console.
57
     *
58
     * @return \Cake\Console\ConsoleOptionParser
59
     */
60
    // @codingStandardsIgnoreStart
61
    public function getOptionParser()
62
    {
63
        // @codingStandardsIgnoreEnd
64
        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 64 which is incompatible with the return type of the parent method Cake\Console\Shell::getOptionParser of type Cake\Console\ConsoleOptionParser.
Loading history...
Deprecated Code introduced by
The method Cake\Console\ConsoleOptionParser::description() has been deprecated with message: 3.4.0 Use setDescription()/getDescription() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
65
            'all',
66
            [
67
                'short' => 'a',
68
                // @codingStandardsIgnoreStart
69
                'help' => __('Searches and pregenerates all async tables it finds.'),
70
                // @codingStandardsIgnoreEnd
71
            ]
72
        // @codingStandardsIgnoreStart
73
        )->description(__('Async table pregenerator'));
74
        // @codingStandardsIgnoreEnd
75
    }
76
77
    protected function setupIterator($path)
78
    {
79
        return new \RegexIterator(new \RecursiveIteratorIterator(
80
            new \RecursiveDirectoryIterator(
81
                $path,
82
                \FilesystemIterator::KEY_AS_PATHNAME |
83
                \FilesystemIterator::CURRENT_AS_FILEINFO |
84
                \FilesystemIterator::SKIP_DOTS
85
            ),
86
            \RecursiveIteratorIterator::CHILD_FIRST,
87
            \RecursiveIteratorIterator::CATCH_GET_CHILD
88
        ), '/.*?.php$/', \RegexIterator::GET_MATCH);
89
    }
90
}
91