WithTaxNoFilterIterator::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 10
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace Germania\VatIdNo\Filters;
3
4
use Germania\VatIdNo\VatIdNoProviderInterface;
5
6 View Code Duplication
class WithTaxNoFilterIterator extends \FilterIterator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
7
{
8
    public $field = "taxno";
9
10 24
    public function __construct(\Traversable $iterator, $field = null)
11
    {
12 24
        if ($iterator instanceof \Iterator) {
13 12
            parent::__construct($iterator);
14
        } else {
15 12
            parent::__construct($iterator->getIterator());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Traversable as the method getIterator() does only exist in the following implementations of said interface: ArrayAccessible, ArrayObject, Doctrine\Common\Collections\AbstractLazyCollection, Doctrine\Common\Collections\ArrayCollection, GuzzleHttp\Cookie\CookieJar, GuzzleHttp\Cookie\FileCookieJar, GuzzleHttp\Cookie\SessionCookieJar, PHPUnit\Framework\DataProviderTestSuite, PHPUnit\Framework\TestSuite, PharIo\Manifest\AuthorCollection, PharIo\Manifest\BundledComponentCollection, PharIo\Manifest\RequirementCollection, PhpCsFixer\Finder, SebastianBergmann\CodeCoverage\Node\Directory, Symfony\Component\Config\Resource\GlobResource, Symfony\Component\Console\Helper\HelperSet, Symfony\Component\Console\Helper\TableRows, Symfony\Component\Consol...lper\AutocompleteValues, Symfony\Component\EventDispatcher\GenericEvent, Symfony\Component\Finder\Finder, Symfony\Component\Finder\Iterator\SortableIterator, Symfony\Component\Finder...\ClassThatInheritFinder, Symfony\Component\Process\InputStream, Symfony\Component\Process\PhpProcess, Symfony\Component\Process\Process, TestIteratorAggregate, TestIteratorAggregate2, WrapperIteratorAggregate, phpDocumentor\Reflection\Types\Compound.

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...
16
        }
17
18 24
        $this->field = $field ?: $this->field;
19 24
    }
20
21 16
    public function accept()
22
    {
23 16
        $current = $this->getInnerIterator()->current();
24
25 16
        if ($current instanceof VatIdNoProviderInterface) {
26 16
            return !empty($current->getTaxNo());
27 16
        } elseif (is_array($current)) {
28 16
            return array_key_exists($this->field, $current)
29 16
              and !empty($current[ $this->field ]);
30 16
        } elseif (is_object($current)) {
31 16
            return isset($current->{$this->field})
32 16
              and !empty($current->{$this->field});
33
        }
34
35 8
        return false;
36
    }
37
}
38