Test Failed
Push — master ( d3f723...421eba )
by Alexis
02:13 queued 14s
created

QueryCountClient::checkQueryCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 9
cp 0
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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
class QueryCountClient extends KernelBrowser
31
{
32
    /*
33
     * We use trait only because of Client::request signature strict type mismatch between Symfony 3 and 4.
34
     */
35
    use QueryCountClientTrait;
36
37
    /** @var QueryCounter */
38
    private $queryCounter;
39
40
    public function setQueryCounter(QueryCounter $queryCounter): void
41
    {
42
        $this->queryCounter = $queryCounter;
43
    }
44
45
    private function checkQueryCount(): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
46
    {
47
        if ($this->getProfile()) {
48
            $this->queryCounter->checkQueryCount(
49
                $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...
50
            );
51
        } else {
52
            // @codeCoverageIgnoreStart
53
            echo "\n".
54
                'Profiler is disabled, it must be enabled for the '.
55
                'Query Counter. '.
56
                'See https://github.com/liip/LiipFunctionalTestBundle#query-counter'.
57
                "\n";
58
            // @codeCoverageIgnoreEnd
59
        }
60
    }
61
}
62