UserConfig::findClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Part of Cli for CodeIgniter
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2015 Kenji Suzuki
8
 * @link       https://github.com/kenjis/codeigniter-cli
9
 */
10
11
namespace Kenjis\CodeIgniter_Cli;
12
13
use Aura\Di\Container;
14
15
class UserConfig
16
{
17
    public static function registerCommandClasses(Container $di, $ci, array $paths)
18
    {
19
        foreach ($paths as $path) {
20
            foreach (glob($path . '*Command.php') as $file) {
21
                $classname = static::findClass($di, $file);
22
                if ($classname === '') {
23
                    break;
24
                }
25
26
                $di->params[$classname] = [
27
                    'context' => $di->lazyGet('aura/cli-kernel:context'),
28
                    'stdio'   => $di->lazyGet('aura/cli-kernel:stdio'),
29
                    'ci'      => $ci,
30
                ];
31
            }
32
        }
33
    }
34
35
    /**
36
     * @param string $file
37
     * @return string classname, if not found returns ''
38
     */
39
    protected static function findClass(Container $di, $file)
40
    {
41
        require_once $file;
42
        $classname = basename($file, '.php');
43
        if (! class_exists($classname)) {
44
            $stdio = $di->get('aura/cli-kernel:stdio');
45
            $stdio->errln(
46
                '<<red>>No such class: ' . $classname . ' in ' . $file . '<<reset>>'
47
            );
48
            return '';
49
        }
50
        return $classname;
51
    }
52
53
    public static function registerCommands(
54
        Container $di, $dispatcher, $help_service, array $paths
55
    )
56
    {
57
        foreach ($paths as $path) {
58 View Code Duplication
            foreach (glob($path . '*Command.php') as $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
                $classname = static::findClass($di, $file);
60
                if ($classname === '') {
61
                    break;
62
                }
63
64
                $command_name = strtolower(basename($classname, 'Command'));
65
                $dispatcher->setObject(
66
                    $command_name,
67
                    $di->lazyNew($classname)
68
                );
69
            }
70
71 View Code Duplication
            foreach (glob($path . '*CommandHelp.php') as $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
                $classname = static::findClass($di, $file);
73
                if ($classname === '') {
74
                    break;
75
                }
76
77
                $command_name = strtolower(basename($classname, 'CommandHelp'));
78
                $help_service->set(
79
                    $command_name,
80
                    $di->lazyNew($classname)
81
                );
82
            }
83
        }
84
    }
85
}
86