Completed
Push — master ( 462877...657388 )
by grégoire
01:55
created

InspectorTrait::executeSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
/*
3
 * This file is part of the Pomm package.
4
 *
5
 * (c) 2014 - 2015 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation\Inspector;
11
12
use PommProject\Foundation\Where;
13
use PommProject\Foundation\Session\Session;
14
use PommProject\Foundation\ConvertedResultIterator;
15
16
/**
17
 * InspectorTrait
18
 *
19
 * Common fonctions for inspectors.
20
 *
21
 * @package     Pomm
22
 * @copyright   2015 Grégoire HUBERT
23
 * @author      Grégoire HUBERT
24
 * @license     X11 {@link http://opensource.org/licenses/mit-license.php}
25
 */
26
trait InspectorTrait
27
{
28
29
    /**
30
     * getSession
31
     *
32
     * This trait should be used by Session clients.
33
     *
34
     * @return Session
35
     */
36
    abstract protected function getSession();
37
38
    /**
39
     * executeSql
40
     *
41
     * Launch query execution expanding string {condition} with the Where query
42
     * parameter and values.
43
     *
44
     * @param  string         $sql
45
     * @param  Where          $condition
46
     * @return ConvertedResultIterator
47
     */
48
    protected function executeSql($sql, Where $condition = null)
49
    {
50
        $condition = (new Where())->andWhere($condition);
51
        $sql = strtr($sql, ['{condition}' => $condition]);
52
53
        return $this
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PommProject\Foundation\Client\ClientInterface as the method query() does only exist in the following implementations of said interface: PommProject\Foundation\P...ry\PreparedQueryManager, PommProject\Foundation\Q...ager\QueryManagerClient, PommProject\Foundation\Q...ager\SimpleQueryManager.

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
            ->getSession()
55
            ->getClientUsingPooler(
56
                'query_manager',
57
                null
58
            )
59
            ->query($sql, $condition->getValues())
60
            ;
61
    }
62
}
63