Completed
Pull Request — master (#12)
by Alexandre
08:14
created

ApplicationTester::getInputStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
use Symfony\Component\Console\Application;
4
use Symfony\Component\Console\Input\StringInput;
5
use Symfony\Component\Console\Output\StreamOutput;
6
7
/**
8
 * A console application tester heavily inspired by/proudly stolen from \Symfony\Component\Console\Tester\ApplicationTester.
9
 */
10 View Code Duplication
class ApplicationTester
0 ignored issues
show
Duplication introduced by
This class 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...
11
{
12
    /**
13
     * @var Application $application
14
     */
15
    private $application;
16
17
    /**
18
     * @var StringInput $input
19
     */
20
    private $input;
21
22
    /**
23
     * @var StreamOutput $output
24
     */
25
    private $output;
26
27
    /**
28
     * @var resource $inputStream
29
     */
30
    private $inputStream;
31
32
    /**
33
     * @param Application $application
34
     */
35
    public function __construct(Application $application)
36
    {
37
        $this->application = $application;
38
    }
39
40
    /**
41
     * @param string $input
42
     * @param array  $options
43
     *
44
     * @return integer
45
     */
46
    public function run($input, array $options = array())
47
    {
48
        if (isset($options['interactive']) && $options['interactive']) {
49
            $this->input = new InteractiveStringInput($input);
50
        } else {
51
            $this->input = new StringInput($input);
52
            $this->input->setInteractive(false);
53
        }
54
55
        $this->output = new StreamOutput(fopen('php://memory', 'w', false));
56
        if (isset($options['decorated'])) {
57
            $this->output->setDecorated($options['decorated']);
58
        }
59
        if (isset($options['verbosity'])) {
60
            $this->output->setVerbosity($options['verbosity']);
61
        }
62
63
        $inputStream = $this->getInputStream();
64
        rewind($inputStream);
65
        $this->application->getHelperSet()
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 setInputStream() does only exist in the following implementations of said interface: 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...
66
            ->get('question')
67
            ->setInputStream($inputStream);
68
69
        return $this->application->run($this->input, $this->output);
70
    }
71
72
    /**
73
     * @param boolean
74
     *
75
     * @return string
76
     */
77
    public function getDisplay($normalize = false)
78
    {
79
        rewind($this->output->getStream());
80
81
        $display = stream_get_contents($this->output->getStream());
82
83
        if ($normalize) {
84
            $display = str_replace(PHP_EOL, "\n", $display);
85
        }
86
87
        return $display;
88
    }
89
90
    /**
91
     * @return InputInterface
92
     */
93
    public function getInput()
94
    {
95
        return $this->input;
96
    }
97
98
    /**
99
     * @return OutputInterface
100
     */
101
    public function getOutput()
102
    {
103
        return $this->output;
104
    }
105
106
    /**
107
     * @param string $input
108
     */
109
    public function putToInputStream($input)
110
    {
111
        fputs($this->getInputStream(), $input);
112
    }
113
114
    /**
115
     * @return resource
116
     */
117
    private function getInputStream()
118
    {
119
        if (null === $this->inputStream) {
120
            $this->inputStream = fopen('php://memory', 'r+', false);
121
        }
122
123
        return $this->inputStream;
124
    }
125
}
126