Completed
Push — master ( be855c...5f2aa4 )
by Nils
10:52
created

ReporterExtension::handleExceptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeReporter;
4
5
use PhmLabs\Components\Init\Init;
6
use Psr\Http\Message\ResponseInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use whm\Smoke\Config\Configuration;
9
use whm\Smoke\Extensions\SmokeReporter\Reporter\ExceptionAwareReporter;
10
use whm\Smoke\Extensions\SmokeReporter\Reporter\Reporter;
11
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever;
12
use whm\Smoke\Rules\CheckResult;
13
14
class ReporterExtension
15
{
16
    /**
17
     * @var Reporter[]
18
     */
19
    private $reporters = array();
20
    private $output;
21
22
    /**
23
     * @var Configuration
24
     */
25
    private $config;
26
27
    public function init(OutputInterface $_output, Configuration $_configuration)
28
    {
29
        $this->output = $_output;
30
        $this->config = $_configuration;
31
32
        if ($_configuration->hasSection('reporter')) {
33
            $this->reporters = Init::initializeAll($_configuration->getSection('reporter'));
0 ignored issues
show
Documentation Bug introduced by
It seems like \PhmLabs\Components\Init...getSection('reporter')) of type array<integer,object<Phm...mponents\Init\objects>> is incompatible with the declared type array<integer,object<whm...ter\Reporter\Reporter>> of property $reporters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
        }
35
    }
36
37
    /**
38
     * @Event("Scanner.Init.ResponseRetriever")
39
     */
40
    public function getResponseRetriever(Retriever $responseRetriever)
41
    {
42
        foreach ($this->reporters as $reporter) {
43
            if (method_exists($reporter, 'setResponseRetriever')) {
44
                $reporter->setResponseRetriever($responseRetriever);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface whm\Smoke\Extensions\Smo...orter\Reporter\Reporter as the method setResponseRetriever() does only exist in the following implementations of said interface: whm\Smoke\Extensions\Smo...er\Reporter\CliReporter, whm\Smoke\Extensions\Smo...porter\KoalamonReporter, whm\Smoke\Extensions\Smo...r\Reporter\LiveReporter, whm\Smoke\Extensions\Smo...ter\StandardCliReporter, whm\Smoke\Extensions\Smo...\Reporter\XUnitReporter.

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...
45
            }
46
        }
47
    }
48
49
    /**
50
     * @var CheckResult[]
51
     *
52
     * @Event("Scanner.Scan.Validate")
53
     */
54
    public function process($results, ResponseInterface $response)
55
    {
56
        foreach ($this->reporters as $reporter) {
57
            $reporter->processResults($results, $response);
58
        }
59
    }
60
61
    /**
62
     * @Event("Scanner.Scan.Finish")
63
     */
64
    public function finish()
65
    {
66
        foreach ($this->reporters as $reporter) {
67
            $reporter->finish();
68
        }
69
    }
70
71
    /**
72
     * @Event("Scanner.Scan.Exceptions.Handle")
73
     */
74
    public function handleExceptions($occuredExceptions)
75
    {
76
        foreach ($this->reporters as $reporter) {
77
            if ($reporter instanceof ExceptionAwareReporter) {
78
                $reporter->handleExceptions($occuredExceptions);
79
            }
80
        }
81
    }
82
}
83