WithoutTaxNoFilterIterator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 33
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 33
loc 33
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 3
A accept() 16 16 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Germania\VatIdNo\Filters;
3
4
use Germania\VatIdNo\VatIdNoProviderInterface;
5
6 View Code Duplication
class WithoutTaxNoFilterIterator extends \FilterIterator
0 ignored issues
show
Duplication introduced by Webdev Walter
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 Webdev Walter
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
22 16
    public function accept()
23
    {
24 16
        $current = $this->getInnerIterator()->current();
25
26 16
        if ($current instanceof VatIdNoProviderInterface) {
27 16
            return empty($current->getTaxNo());
28 16
        } elseif (is_array($current)) {
29 16
            return !array_key_exists($this->field, $current)
30 16
               or empty($current[ $this->field ]);
31 16
        } elseif (is_object($current)) {
32 16
            return !isset($current->{$this->field})
33 16
                or empty($current->{$this->field});
34
        }
35
36 8
        return true;
37
    }
38
}
39