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.

ParameterCommand::configure()   A
last analyzed

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
namespace Naneau\FileGen\Test\Console;
3
4
use Naneau\FileGen\Structure;
5
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * A simple command that uses the filegenParameters helper to ask for
12
 * parameters for a supplied Structure
13
 */
14
class ParameterCommand extends Command
15
{
16
    /**
17
     * The structure
18
     *
19
     * @var Structure
20
     **/
21
    private $structure;
22
23
    /**
24
     * the receive params
25
     *
26
     * @var array
27
     **/
28
    private $received = array();
29
30
    /**
31
     * Configure the command
32
     *
33
     * @return void
34
     **/
35
    protected function configure()
36
    {
37
        $this->setName('filegen:test:filegen-parameters');
38
    }
39
40
    /**
41
     * Get the structure
42
     *
43
     * @return Structure
44
     */
45
    public function getStructure()
46
    {
47
        return $this->structure;
48
    }
49
50
    /**
51
     * The structure
52
     *
53
     * @param  Structure        $structure
54
     * @return ParameterCommand
55
     */
56
    public function setStructure(Structure $structure)
57
    {
58
        $this->structure = $structure;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Get the received parameters
65
     *
66
     * @return array
67
     */
68
    public function getReceived()
69
    {
70
        return $this->received;
71
    }
72
73
    /**
74
     * Set the received parameters
75
     *
76
     * @param array $received
77
     * @return ParameterCommand
78
     */
79
    public function setReceived(array $received)
80
    {
81
        $this->received = $received;
82
83
        return $this;
84
    }
85
86
    /**
87
     * Execute the command
88
     *
89
     * @param  InputInterface  $input
90
     * @param  OutputInterface $output
91
     * @return void
92
     **/
93
    protected function execute(InputInterface $input, OutputInterface $output)
94
    {
95
        $received = $this->getHelper('filegenParameters')->askParameters(
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 askParameters() does only exist in the following implementations of said interface: Naneau\FileGen\Console\Helper\ParameterHelper.

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...
96
            $this->getStructure(),
97
            $input,
98
            $output
99
        );
100
101
        $this->setReceived($received);
102
    }
103
}
104