Completed
Push — master ( 284085...99e334 )
by Fumio
02:01
created

AddonStatusCommand::dump()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel\Commands;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Finder\Finder;
7
use Illuminate\Filesystem\Filesystem;
8
use Jumilla\Addomnipot\Laravel\Environment as AddonEnvironment;
9
use UnexpectedValueException;
10
11
class AddonStatusCommand extends Command
12
{
13
    use Functions;
14
15
    /**
16
     * The console command signature.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'addon:status
21
        {addon? : Name of addon.}
22
    ';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Addons status';
30
31
    /**
32
     * Execute the console command.
33
     *
34
     * @return mixed
35
     */
36 2
    public function handle(Filesystem $filesystem, AddonEnvironment $env)
37
    {
38
        // make addons/
39 2
        $addons_directory = $env->path();
40 2
        if (!$filesystem->exists($addons_directory)) {
41
            $filesystem->makeDirectory($addons_directory);
42
        }
43
44 2
        $addon_name = $this->argument('addon');
45 2
        if (! $addon_name) {
46 1
            $addons = $env->addons();
47
48 1
            $this->line('--------');
49 1
            foreach ($addons as $addon) {
50 1
                $this->dump($addon);
51 1
                $this->line('--------');
52
            }
53
        }
54
        else {
55 1
            $addon = $env->addon($addon_name);
56
57
            // check addon
58 1
            if ($addon === null) {
59
                throw new UnexpectedValueException(sprintf('Addon "%s" is not found.', $addon_name));
60
            }
61
62 1
            $this->dump($addon);
63
        }
64 2
    }
65
66 2
    protected function dump($addon)
67
    {
68 2
        $this->dumpProperties($addon);
69 2
        $this->dumpClasses($addon);
70 2
        $this->dumpServiceProviders($addon);
71 2
    }
72
73 2
    protected function dumpProperties($addon)
74
    {
75 2
        $this->info(sprintf('Addon "%s"', $addon->name()));
76 2
        $this->info(sprintf('Path: %s', $addon->relativePath($this->laravel)));
77 2
        $this->info(sprintf('PHP namespace: %s', $addon->phpNamespace()));
78 2
    }
79
80 2
    protected function dumpClasses($addon)
81
    {
82
        // load laravel services
83 2
        $files = $this->laravel['files'];
0 ignored issues
show
Unused Code introduced by
$files is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84 2
        $env = $this->laravel[AddonEnvironment::class];
85
86
        // 全ディレクトリ下を探索する (PSR-4)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
87 2
        foreach ($addon->config('addon.directories', []) as $directory) {
88
            $this->info(sprintf('PHP classes on "%s"', $directory));
89
90
            $classDirectoryPath = $addon->path($directory);
91
92
            if (!file_exists($classDirectoryPath)) {
93
                $this->line(sprintf('Warning: Class directory "%s" not found', $directory));
94
                continue;
95
            }
96
97
            // recursive find files
98
            $phpFilePaths = iterator_to_array((new Finder())->in($classDirectoryPath)->name('*.php')->files(), false);
99
100
            foreach ($phpFilePaths as $phpFilePath) {
101
                $relativePath = substr($phpFilePath, strlen($classDirectoryPath) + 1);
102
103
                $classFullName = $addon->phpNamespace().'\\'.$env->pathToClass($relativePath);
104
105
                $this->line(sprintf('  "%s" => %s', $relativePath, $classFullName));
106
            }
107
        }
108 2
    }
109
110 2
    protected function dumpServiceProviders($addon)
0 ignored issues
show
Unused Code introduced by
The parameter $addon is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112 2
    }
113
}
114