Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

Console   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 54
rs 10
c 1
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A action() 0 15 3
1
<?php /** MicroConsole */
2
3
namespace Micro\Cli;
4
5
use Micro\Base\IContainer;
6
7
/**
8
 * Console class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/lugnsk/micro
12
 * @copyright Copyright &copy; 2013 Oleg Lunegov
13
 * @license /LICENSE
14
 * @package Micro
15
 * @subpackage Cli
16
 * @version 1.0
17
 * @since 1.0
18
 * @abstract
19
 */
20
class Console
21
{
22
    /** @var IContainer $container */
23
    protected $container;
24
    /** @var string $command Parsed command */
25
    protected $command;
26
    /** @var array $args Arguments from params */
27
    protected $args = [];
28
29
    /**
30
     * Constructor command
31
     *
32
     * @access public
33
     *
34
     * @param IContainer $container
35
     *
36
     * @result void
37
     */
38
    public function __construct(IContainer $container)
39
    {
40
        $this->container = $container;
41
42
        foreach ($container->request->getArguments() AS $param) {
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
43
            if ($pos = strpos($param, '=')) {
44
                $this->args[substr($param, 0, $pos)] = substr($param, $pos + 1);
45
            }
46
        }
47
    }
48
49
    /**
50
     * Run action of console command by name
51
     *
52
     * @access public
53
     *
54
     * @param string $name Command name
55
     *
56
     * @return bool|ConsoleCommand|string
57
     */
58
    public function action($name)
59
    {
60
        $command = '\\Micro\\Cli\\Consoles\\' . $name . 'ConsoleCommand';
61
        $command = class_exists($command) ? $command : '\\App\\Consoles\\' . $name . 'ConsoleCommand';
62
63
        if (!class_exists($command)) {
64
            return false;
65
        }
66
67
        /** @var ConsoleCommand $command */
68
        $command = new $command (['container' => $this->container, 'args' => $this->args]);
69
        $command->execute();
70
71
        return $command;
72
    }
73
}
74