WithVatIdNoFilterIterator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 31
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 31
loc 31
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() 15 15 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 WithVatIdNoFilterIterator 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 = "vatin";
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->getVatIdNo());
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 8
        return false;
35
    }
36
}
37