Completed
Push — wip-platform ( 03cba6...2999ab )
by
unknown
05:53 queued 02:49
created

Interaction::askAndValidate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 3
Ratio 17.65 %

Importance

Changes 0
Metric Value
dl 3
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 5
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\Command\Traits;
13
14
use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper;
15
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\ConfirmationQuestion;
19
use Symfony\Component\Console\Question\Question;
20
21
/**
22
 * This trait is the perfect copy of the Blast\Bundle\OuterExtensionBundle\Command\Traits\Interaction
23
 * They are still distinct because of the few amount of code to factorize, but it will be done
24
 * as soon as we have a bit more to make generic.
25
 **/
26
trait Interaction
27
{
28
    /**
29
     * @param OutputInterface $output
30
     * @param string          $message
31
     */
32
    private function writeError(OutputInterface $output, $message)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
33
    {
34
        $output->writeln(sprintf("\n<error>%s</error>", $message));
35
    }
36
37
    /**
38
     * @param InputInterface  $input
39
     * @param OutputInterface $output
40
     * @param string          $questionText
41
     * @param mixed           $default
42
     * @param callable        $validator
43
     *
44
     * @return mixed
45
     */
46
    private function askAndValidate(InputInterface $input, OutputInterface $output, $questionText, $default = null, $validator = null)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
47
    {
48
        $questionHelper = $this->getQuestionHelper();
49
50
        // NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
51 View Code Duplication
        if ($questionHelper instanceof DialogHelper) {
0 ignored issues
show
Bug introduced by
The class Sensio\Bundle\GeneratorB...and\Helper\DialogHelper does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
52
            return $questionHelper->askAndValidate($output, $questionHelper->getQuestion($questionText, $default), $validator, false, $default);
53
        }
54
55
        $question = new Question($questionHelper->getQuestion($questionText, $default), $default);
56
57
        if (null !== $validator) {
58
            $question->setValidator($validator);
59
        }
60
61
        return $questionHelper->ask($input, $output, $question);
62
    }
63
64
    /**
65
     * @param InputInterface  $input
66
     * @param OutputInterface $output
67
     * @param string          $questionText
68
     * @param string          $default
69
     * @param string          $separator
70
     *
71
     * @return string
72
     */
73 View Code Duplication
    private function askConfirmation(InputInterface $input, OutputInterface $output, $questionText, $default, $separator)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
74
    {
75
        $questionHelper = $this->getQuestionHelper();
76
77
        // NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
78
        if ($questionHelper instanceof DialogHelper) {
0 ignored issues
show
Bug introduced by
The class Sensio\Bundle\GeneratorB...and\Helper\DialogHelper does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
79
            $question = $questionHelper->getQuestion($questionText, $default, $separator);
80
81
            return $questionHelper->askConfirmation($output, $question, ($default === 'no' ? false : true));
82
        }
83
84
        $question = new ConfirmationQuestion($questionHelper->getQuestion(
85
            $questionText,
86
            $default,
87
            $separator
88
        ), ($default === 'no' ? false : true));
89
90
        return $questionHelper->ask($input, $output, $question);
91
    }
92
93
    /**
94
     * @return QuestionHelper|DialogHelper
95
     */
96 View Code Duplication
    private function getQuestionHelper()
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...
97
    {
98
        // NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
99
        if (class_exists('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper')) {
100
            $questionHelper = $this->getHelper('dialog');
0 ignored issues
show
Bug introduced by
It seems like getHelper() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
101
102
            if (!$questionHelper instanceof DialogHelper) {
0 ignored issues
show
Bug introduced by
The class Sensio\Bundle\GeneratorB...and\Helper\DialogHelper does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
103
                $questionHelper = new DialogHelper();
104
                $this->getHelperSet()->set($questionHelper);
0 ignored issues
show
Bug introduced by
It seems like getHelperSet() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
105
            }
106
        } else {
107
            $questionHelper = $this->getHelper('question');
0 ignored issues
show
Bug introduced by
It seems like getHelper() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
108
109
            if (!$questionHelper instanceof QuestionHelper) {
110
                $questionHelper = new QuestionHelper();
111
                $this->getHelperSet()->set($questionHelper);
0 ignored issues
show
Bug introduced by
It seems like getHelperSet() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
112
            }
113
        }
114
115
        return $questionHelper;
116
    }
117
}
118