Completed
Push — develop ( 9d1b56...420894 )
by
unknown
11s
created

Paginator::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 * @author Miroslav Fedeleš <[email protected]>
9
 * @since 0.27
10
 */
11
namespace Solr\Paginator;
12
13
use Zend\Paginator\Exception\InvalidArgumentException;
14
use Solr\FacetsProviderInterface;
15
16
class Paginator extends \Zend\Paginator\Paginator implements FacetsProviderInterface
17
{
18
19
    /**
20
     * @see \Zend\Paginator\Paginator::__construct()
21
     */
22
    public function __construct($adapter)
23
    {
24
        if (!$adapter instanceof FacetsProviderInterface) {
25
            throw new InvalidArgumentException(sprintf('adapter must implement %s interface', FacetsProviderInterface::class));
26
        }
27
        
28
        parent::__construct($adapter);
0 ignored issues
show
Documentation introduced by
$adapter is of type object<Solr\FacetsProviderInterface>, but the function expects a object<Zend\Paginator\Ad...pterAggregateInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
29
    }
30
31
    /**
32
     * @see \Solr\FacetsProviderInterface::getFacets()
33
     */
34
    public function getFacets()
35
    {
36
        return $this->adapter->getFacets();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Paginator\Adapter\AdapterInterface as the method getFacets() does only exist in the following implementations of said interface: Solr\Paginator\Adapter\SolrAdapter.

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...
37
    }
38
}