Filter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 14.29%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 34
ccs 1
cts 7
cp 0.1429
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 3
A accept() 0 4 1
1
<?php
2
/**
3
 * Filter.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Widgets!
9
 * @subpackage     Filters
10
 * @since          1.0.0
11
 *
12
 * @date           26.06.15
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Widgets\Filters\Priority;
18
19
use IPub\Widgets\Filters;
20
21
/**
22
 * Widgets priority filter
23
 *
24
 * @package        iPublikuj:Widgets!
25
 * @subpackage     Filters
26
 *
27
 * @author         Adam Kadlec <[email protected]>
28
 */
29 1
class Filter extends Filters\FilterIterator
30
{
31
	/**
32
	 * {@inheritdoc}
33
	 */
34
	public function __construct(\Iterator $iterator, array $options = [])
35
	{
36
		/** @var  $elements */
37
		$elements = iterator_to_array($iterator, FALSE);
38
39
		$iterator->uasort(function ($a, $b) use ($elements) : int {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Iterator as the method uasort() does only exist in the following implementations of said interface: ArrayIterator, RecursiveArrayIterator.

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...
40
41
			$priorityA = (int) $a->getPriority();
42
			$priorityB = (int) $b->getPriority();
43
44
			if ($priorityA == $priorityB) {
45
				$priorityA = array_search($a, $elements);
46
				$priorityB = array_search($b, $elements);
47
			}
48
49
			return ($priorityA < $priorityB) ? -1 : 1;
50
		});
51
52
		parent::__construct($iterator, $options);
53
	}
54
55
	/**
56
	 * {@inheritdoc}
57
	 */
58
	public function accept() : bool
59
	{
60
		return TRUE;
61
	}
62
}
63