Completed
Push — wip-subtree ( 04b705...405079 )
by
unknown
12:15
created

Interaction::askConfirmation()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 2
nop 5
1
<?php
2
3
/*
4
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\Bundle\CoreBundle\Command\Traits;
14
15
use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper;
16
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Question\ConfirmationQuestion;
20
use Symfony\Component\Console\Question\Question;
21
22
/**
23
 * This trait is the perfect copy of the Blast\Bundle\OuterExtensionBundle\Command\Traits\Interaction
24
 * They are still distinct because of the few amount of code to factorize, but it will be done
25
 * as soon as we have a bit more to make generic.
26
 **/
27
trait Interaction
28
{
29
    /**
30
     * @param OutputInterface $output
31
     * @param string          $message
32
     */
33
    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...
34
    {
35
        $output->writeln(sprintf("\n<error>%s</error>", $message));
36
    }
37
38
    /**
39
     * @param InputInterface  $input
40
     * @param OutputInterface $output
41
     * @param string          $questionText
42
     * @param mixed           $default
43
     * @param callable        $validator
44
     *
45
     * @return mixed
46
     */
47
    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...
48
    {
49
        $questionHelper = $this->getQuestionHelper();
50
51
        // NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
52 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...
53
            return $questionHelper->askAndValidate($output, $questionHelper->getQuestion($questionText, $default), $validator, false, $default);
54
        }
55
56
        $question = new Question($questionHelper->getQuestion($questionText, $default), $default);
57
58
        if (null !== $validator) {
59
            $question->setValidator($validator);
60
        }
61
62
        return $questionHelper->ask($input, $output, $question);
63
    }
64
65
    /**
66
     * @param InputInterface  $input
67
     * @param OutputInterface $output
68
     * @param string          $questionText
69
     * @param string          $default
70
     * @param string          $separator
71
     *
72
     * @return string
73
     */
74 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...
75
    {
76
        $questionHelper = $this->getQuestionHelper();
77
78
        // NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
79
        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...
80
            $question = $questionHelper->getQuestion($questionText, $default, $separator);
81
82
            return $questionHelper->askConfirmation($output, $question, ($default === 'no' ? false : true));
83
        }
84
85
        $question = new ConfirmationQuestion($questionHelper->getQuestion(
86
            $questionText,
87
            $default,
88
            $separator
89
        ), ($default === 'no' ? false : true));
90
91
        return $questionHelper->ask($input, $output, $question);
92
    }
93
94
    /**
95
     * @return QuestionHelper|DialogHelper
96
     */
97 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...
98
    {
99
        // NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
100
        if (class_exists('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper')) {
101
            $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...
102
103
            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...
104
                $questionHelper = new DialogHelper();
105
                $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...
106
            }
107
        } else {
108
            $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...
109
110
            if (!$questionHelper instanceof QuestionHelper) {
111
                $questionHelper = new QuestionHelper();
112
                $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...
113
            }
114
        }
115
116
        return $questionHelper;
117
    }
118
}
119