GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d24bd4...975d4c )
by
unknown
07:32 queued 02:44
created

src/Task/AnswerTask.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * See class comment
4
 *
5
 * PHP Version 5
6
 *
7
 * @category   Netresearch
8
 * @package    Netresearch\Kite
9
 * @subpackage Task
10
 * @author     Christian Opitz <[email protected]>
11
 * @license    http://www.netresearch.de Netresearch Copyright
12
 * @link       http://www.netresearch.de
13
 */
14
15
namespace Netresearch\Kite\Task;
16
use Netresearch\Kite\Task;
17
use Symfony\Component\Console\Question\Question;
18
19
/**
20
 * Ask a question and return the answer
21
 *
22
 * @category   Netresearch
23
 * @package    Netresearch\Kite
24
 * @subpackage Task
25
 * @author     Christian Opitz <[email protected]>
26
 * @license    http://www.netresearch.de Netresearch Copyright
27
 * @link       http://www.netresearch.de
28
 */
29
class AnswerTask extends Task
30
{
31
    /**
32
     * Configure the variables
33
     *
34
     * @return array
35
     */
36 View Code Duplication
    protected function configureVariables()
37
    {
38
        return array(
39
            'question' => array(
40
                'type' => 'string',
41
                'required' => true,
42
                'label' => 'The question to ask'
43
            ),
44
            'default' => array(
45
                'type' => 'string|numeric',
46
                'label' => 'Default value (shown to the user as well)'
47
            ),
48
            '--'
49
        ) + parent::configureVariables();
50
    }
51
52
    /**
53
     * Format a question
54
     *
55
     * @param string $question The question
56
     * @param mixed  $default  Default value
57
     *
58
     * @return string
59
     */
60
    protected function formatQuestion($question, $default = null)
61
    {
62
        return '<question>' . $question . '</question> '
63
        . ($default !== null && $default !== '' ? "[{$default}] " : '');
64
    }
65
66
    /**
67
     * Create a question
68
     *
69
     * @param string $question The question
70
     * @param mixed  $default  Default value
71
     *
72
     * @return Question
73
     */
74
    protected function createQuestion($question, $default)
75
    {
76
        return new Question($this->formatQuestion($question, $default), $default);
77
    }
78
79
    /**
80
     * Execute the task
81
     *
82
     * @return mixed
83
     */
84
    public function execute()
85
    {
86
        $answer = $this->console->getHelper('question')->ask(
0 ignored issues
show
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\DialogHelper, 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...
87
            $this->console->getInput(),
88
            $this->console->getOutput(),
89
            $this->createQuestion($this->get('question'), $this->get('default', null))
90
        );
91
92
        return $answer;
93
    }
94
}
95
?>
96