Completed
Push — master ( d5cda2...f735ec )
by Martijn van
02:14
created

Application::output()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
use Symfony\Component\Console\Input\ArrayInput;
4
use Symfony\Component\Console\Input\InputOption;
5
use Symfony\Component\Console\Output\BufferedOutput;
6
use Symfony\Component\Console\Application as SymfonyApplication;
7
use Symfony\Component\Console\Command\Command as SymfonyCommand;
8
9
/**
10
 * Class Application
11
 *
12
 * Shameless copy/paste from Taylor Otwell's Laravel
13
 */
14
class Application extends SymfonyApplication
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
15
{
16
17
    /**
18
     * The output from the previous command.
19
     *
20
     * @var \Symfony\Component\Console\Output\BufferedOutput
21
     */
22
    protected $lastOutput;
23
24
    public function __construct()
25
    {
26
        parent::__construct();
27
28
        $this->loadCommands();
29
30
        $this->setAutoExit(false);
31
        $this->setCatchExceptions(false);
32
    }
33
34
    /**
35
     * Run an Artisan console command by name.
36
     *
37
     * @param  string  $command
38
     * @param  array  $parameters
39
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be null|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
40
     */
41
    public function call($command, array $parameters = [])
42
    {
43
        $parameters = array_merge((array)$command, $parameters);
44
45
        $this->lastOutput = new BufferedOutput;
46
47
        $this->setCatchExceptions(false);
48
49
        $result = $this->run(new ArrayInput($parameters), $this->lastOutput);
50
51
        $this->setCatchExceptions(true);
52
53
        return $result;
54
    }
55
56
    /**
57
     * Get the output for the last run command.
58
     *
59
     * @return string
60
     */
61
    public function output()
62
    {
63
        return $this->lastOutput ? $this->lastOutput->fetch() : '';
64
    }
65
66
    /**
67
     * Add a command to the console.
68
     *
69
     * @param  \Symfony\Component\Console\Command\Command  $command
70
     * @return \Symfony\Component\Console\Command\Command
0 ignored issues
show
Documentation introduced by
Should the return type not be SymfonyCommand|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
71
     */
72
    public function add(SymfonyCommand $command)
73
    {
74
        return $this->addToParent($command);
75
    }
76
77
    /**
78
     * Add the command to the parent instance.
79
     *
80
     * @param  \Symfony\Component\Console\Command\Command  $command
81
     * @return \Symfony\Component\Console\Command\Command
0 ignored issues
show
Documentation introduced by
Should the return type not be SymfonyCommand|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
82
     */
83
    protected function addToParent(SymfonyCommand $command)
84
    {
85
        return parent::add($command);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (add() instead of addToParent()). Are you sure this is correct? If so, you might want to change this to $this->add().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
86
    }
87
88
    /**
89
     * Get the default input definitions for the applications.
90
     *
91
     * This is used to add the --env option to every available command.
92
     *
93
     * @return \Symfony\Component\Console\Input\InputDefinition
94
     */
95
    protected function getDefaultInputDefinition()
96
    {
97
        $definition = parent::getDefaultInputDefinition();
98
99
        $definition->addOption($this->getEnvironmentOption());
100
101
        return $definition;
102
    }
103
104
    /**
105
     * Get the global environment option for the definition.
106
     *
107
     * @return \Symfony\Component\Console\Input\InputOption
108
     */
109
    protected function getEnvironmentOption()
110
    {
111
        $message = 'The environment the command should run under.';
112
113
        return new InputOption('--env', null, InputOption::VALUE_OPTIONAL, $message);
114
    }
115
116
    /**
117
     * Load all available commands into the console application
118
     */
119
    protected function loadCommands()
120
    {
121
        /**
122
         * Why does this not work
123
         */
124
        $commands = ClassInfo::subclassesFor('SilverstripeCommand'); //var_dump($commands);exit();
0 ignored issues
show
Unused Code introduced by
$commands 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...
Unused Code Comprehensibility introduced by
89% 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...
125
126
        // This works, but does not load custom Commands
127
        $commands = ClassInfo::classes_for_folder(BASE_PATH . '/console/code/');
128
129
        /** @var SilverstripeCommand $command */
130
        foreach ($commands as $command) {
131
            $reflection = new ReflectionClass($command);
132
            if (!$reflection->isAbstract() && $reflection->isSubclassOf('SilverstripeCommand')) {
133
                $this->add(new $command());
134
            }
135
        }
136
    }
137
138
}
139