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.

AnswerTask   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 22.73 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 15
loc 66
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configureVariables() 15 15 1
A formatQuestion() 0 5 3
A createQuestion() 0 4 1
A execute() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        return array(
0 ignored issues
show
Best Practice introduced by
The expression return array('question' ...::configureVariables(); seems to be an array, but some of its elements' types (string) are incompatible with the return type of the parent method Netresearch\Kite\Task::configureVariables of type array<string,array>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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
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\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