ModifiedItemFilter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace TreeHouse\IoBundle\Item\Modifier\Item\Filter;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
use TreeHouse\Feeder\Exception\FilterException;
7
use TreeHouse\Feeder\Modifier\Item\Filter\FilterInterface;
8
use TreeHouse\IoBundle\Import\Feed\FeedItemBag;
9
use TreeHouse\IoBundle\Item\ItemBag;
10
use TreeHouse\IoBundle\Model\SourceInterface;
11
use TreeHouse\IoBundle\Scrape\ScrapedItemBag;
12
use TreeHouse\IoBundle\Source\Manager\CachedSourceManager;
13
14
/**
15
 * Filters out items in the feed that have a modification date
16
 * before its corresponding source's modification date.
17
 */
18 View Code Duplication
class ModifiedItemFilter implements FilterInterface
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...
19
{
20
    /**
21
     * @var CachedSourceManager
22
     */
23
    protected $sourceManager;
24
25
    /**
26
     * @param CachedSourceManager $sourceManager
27
     */
28 10
    public function __construct(CachedSourceManager $sourceManager)
29
    {
30 10
        $this->sourceManager = $sourceManager;
31 10
    }
32
33
    /**
34
     * @inheritdoc
35
     *
36
     * @param ItemBag $item
37
     */
38 10
    public function filter(ParameterBag $item)
39
    {
40
        /** @var FeedItemBag $item */
41
        // if source does not exist yet, by all means process it
42 10
        if (null === $source = $this->findSource($item)) {
0 ignored issues
show
Compatibility introduced by
$item of type object<Symfony\Component...oundation\ParameterBag> is not a sub-type of object<TreeHouse\IoBundle\Item\ItemBag>. It seems like you assume a child class of the class Symfony\Component\HttpFoundation\ParameterBag to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
43 4
            return;
44
        }
45
46
        // first try modification date
47 8
        if (null !== $mutationDate = $item->getDatetimeModified()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\HttpFoundation\ParameterBag as the method getDatetimeModified() does only exist in the following sub-classes of Symfony\Component\HttpFoundation\ParameterBag: TreeHouse\IoBundle\Import\Feed\FeedItemBag, TreeHouse\IoBundle\Item\ItemBag, TreeHouse\IoBundle\Scrape\ScrapedItemBag. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
48 6
            if ($source->getDatetimeImported() > $mutationDate) {
49 4
                throw new FilterException('Item is not modified');
50
            }
51
        }
52
53
        // item is modified or we don't have enough information to determine that, either way continue.
54 4
    }
55
56
    /**
57
     * @param ItemBag $item
58
     *
59
     * @return null|SourceInterface
60
     */
61 10
    protected function findSource(ItemBag $item)
62
    {
63 10
        if ($item instanceof FeedItemBag) {
64 10
            return $this->sourceManager->findSourceByFeed($item->getFeed(), $item->getOriginalId());
65
        }
66
67
        if ($item instanceof ScrapedItemBag) {
68
            return $this->sourceManager->findSourceByScraper($item->getScraper(), $item->getOriginalId());
69
        }
70
71
        return null;
72
    }
73
}
74