HomepageQuestion   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 42
loc 42
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 9 9 2
A buildQuestion() 7 7 1
A buildValidator() 9 9 3

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
namespace Samurai\Project\Question;
3
4
use Samurai\Task\ITask;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Question\Question as SimpleQuestion;
8
9
/**
10
 * Class HomePageQuestion
11
 * @package Samurai\Project\Question
12
 * @author Raphaël Lefebvre <[email protected]>
13
 */
14 View Code Duplication
class HomepageQuestion extends Question
15
{
16
    /**
17
     * @param InputInterface $input
18
     * @param OutputInterface $output
19
     * @return int
20
     */
21 5
    public function execute(InputInterface $input, OutputInterface $output)
22
    {
23
        try {
24 5
            $this->getProject()->setHomepage($this->ask($input, $output, $this->buildQuestion()));
25 5
        }catch(\Exception $e){
26 1
            $this->getProject()->setHomepage('');
27
        }
28 5
        return ITask::NO_ERROR_CODE;
29
    }
30
31
    /**
32
     * @return SimpleQuestion
33
     */
34 5
    private function buildQuestion()
35
    {
36 5
        $question = new SimpleQuestion('<question>Enter your project homepage:</question>');
37 5
        $question->setValidator($this->buildValidator());
38 5
        $question->setMaxAttempts(3);
39 5
        return $question;
40
    }
41
42
    /**
43
     * @return callable
44
     */
45
    private function buildValidator()
46
    {
47 5
        return function ($answer) {
48 3
            if ($answer && !filter_var($answer, FILTER_VALIDATE_URL)) {
49 1
                throw new \RuntimeException('Error: format not valid');
50
            }
51 2
            return $answer;
52 5
        };
53
    }
54
55
}
56