NameQuestion   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 37
loc 37
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

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

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 NameQuestion
11
 * @package Samurai\Project\Question
12
 * @author Raphaël Lefebvre <[email protected]>
13
 */
14 View Code Duplication
class NameQuestion 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 5
        $this->getProject()->setName($this->ask($input, $output, $this->buildQuestion()));
24 3
        return $this->getProject()->getName() ? ITask::NO_ERROR_CODE : ITask::BLOCKING_ERROR_CODE;
25
    }
26
27
    /**
28
     * @return SimpleQuestion
29
     */
30 5
    private function buildQuestion()
31
    {
32 5
        $question = new SimpleQuestion('<question>Enter your project name (<vendor>/<package>):</question>');
33 5
        $question->setValidator($this->buildValidator());
34 5
        $question->setMaxAttempts(3);
35 5
        return $question;
36
    }
37
38
    /**
39
     * @return callable
40
     */
41
    private function buildValidator()
42
    {
43 5
        return function ($answer) {
44 3
            if (!preg_match('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}', $answer)) {
45 2
                throw new \RuntimeException('Error: format not valid');
46
            }
47 1
            return $answer;
48 5
        };
49
    }
50
}
51