Completed
Pull Request — 1.5 (#769)
by Paweł
19:46 queued 09:38
created

MetaLoaderCollector::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Template Engine Bundle.
5
 *
6
 * Copyright 2019 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2019 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\TemplatesSystemBundle\DataCollector;
16
17
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
21
22
class MetaLoaderCollector extends DataCollector
23
{
24
    protected $traceableChainLoader;
25
26
    public function __construct(LoaderInterface $traceableChainLoader)
27
    {
28
        $this->traceableChainLoader = $traceableChainLoader;
29
    }
30
31
    public function collect(Request $request, Response $response, \Exception $exception = null)
32
    {
33
        $data = $this->traceableChainLoader->getData();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SWP\Component\TemplatesS...\Loader\LoaderInterface as the method getData() does only exist in the following implementations of said interface: SWP\Bundle\TemplatesSyst...er\TraceableChainLoader.

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...
34
35
        foreach ($data['calledLoaders'] as $class => $loader) {
36
            $totalDuration = 0;
37
            foreach ($loader['calls'] as $call) {
38
                $totalDuration = $totalDuration + $call['duration'];
39
            }
40
            $loader['totalDuration'] = $totalDuration;
41
42
            $data['calledLoaders'][$class] = $loader;
43
        }
44
45
        $this->data = $data;
46
    }
47
48
    /**
49
     * @return array
50
     */
51
    public function getData()
52
    {
53
        return $this->data;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getName()
60
    {
61
        return 'meta_loader_collector';
62
    }
63
64
    public function reset()
65
    {
66
        return;
67
    }
68
}
69