Completed
Push — master ( 52e441...fcb932 )
by Robbie
9s
created

SilverStripeCommand::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\Console\Command;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Dev\BuildTask;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\Question;
13
14
/**
15
 * A slightly embellished Symfony Command class which is SilverStripe aware
16
 *
17
 * @package silverstripe-console
18
 * @author  Robbie Averill <[email protected]>
19
 */
20
class SilverStripeCommand extends Command
21
{
22
    /**
23
     * Retrieve an argument from the input interface, or use the Question helper to ask for input
24
     * if it wasn't provided. Will automatically hide input for password fields.
25
     *
26
     * @param  InputInterface  $input
27
     * @param  OutputInterface $output
28
     * @param  string          $key      The argument key, e.g. "username"
29
     * @param  string          $question The question to ask, e.g. "Which username: "
30
     * @return string|null
31
     */
32
    protected function getOrAskForArgument(InputInterface $input, OutputInterface $output, $key, $question)
33
    {
34
        if ($supplied = $input->getArgument($key)) {
35
            return $supplied;
36
        }
37
38
        $question = new Question($question);
39
        if (stripos($key, 'password') !== false) {
40
            $question->setHidden(true);
41
            $question->setHiddenFallback(false);
42
        }
43
44
        return $this->getHelper('question')->ask($input, $output, $question);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method ask() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
45
    }
46
47
    /**
48
     * Get the SilverStripe Injector
49
     *
50
     * @return Injector
51
     */
52
    public function getInjector()
53
    {
54
        return Injector::inst();
55
    }
56
57
    /**
58
     * Get the configuration API handler
59
     *
60
     * @return Config
61
     */
62
    public function getConfig()
63
    {
64
        return Config::inst();
65
    }
66
}
67