Completed
Pull Request — master (#560)
by Alexis
12:43
created

QueryCountClient::setQueryCounter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Liip/FunctionalTestBundle
7
 *
8
 * (c) Lukas Kahwe Smith <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Liip\FunctionalTestBundle;
15
16
use Symfony\Bundle\FrameworkBundle\Client;
17
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CompilerDebugDumpPass;
18
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
19
20
// Symfony <4 BC
21
if (class_exists(CompilerDebugDumpPass::class)) {
22
    class_alias(QueryCountClientSymfony3Trait::class, QueryCountClientTrait::class);
23
}
24
25
// Symfony <4.3.1 BC
26
if (!class_exists(KernelBrowser::class)) {
27
    class_alias(Client::class, KernelBrowser::class);
28
}
29
30
if (!class_exists(Client::class)) {
31
    class_alias(KernelBrowser::class, Client::class);
32
}
33
34
class QueryCountClient extends KernelBrowser
35
{
36
    /*
37
     * We use trait only because of Client::request signature strict type mismatch between Symfony 3 and 4.
38
     */
39
    use QueryCountClientTrait;
40
41
    /** @var QueryCounter */
42
    private $queryCounter;
43
44
    public function setQueryCounter(QueryCounter $queryCounter): void
45
    {
46
        $this->queryCounter = $queryCounter;
47
    }
48
49
    private function checkQueryCount(): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
50
    {
51
        if ($this->getProfile()) {
52
            $this->queryCounter->checkQueryCount(
53
                $this->getProfile()->getCollector('db')->getQueryCount()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\HttpKe...\DataCollectorInterface as the method getQueryCount() does only exist in the following implementations of said interface: Doctrine\Bundle\Doctrine...r\DoctrineDataCollector, Symfony\Bridge\Doctrine\...r\DoctrineDataCollector.

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...
54
            );
55
        } else {
56
            // @codeCoverageIgnoreStart
57
            echo "\n".
58
                'Profiler is disabled, it must be enabled for the '.
59
                'Query Counter. '.
60
                'See https://github.com/liip/LiipFunctionalTestBundle#query-counter'.
61
                "\n";
62
            // @codeCoverageIgnoreEnd
63
        }
64
    }
65
}
66