Completed
Push — EZP-25088-search-handler-inter... ( 170b1b...34d2d2 )
by
unknown
18:19
created

CopySubtree   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 39
rs 10
wmc 9
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
C receive() 0 26 7
A canIndexContent() 0 4 2
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Search\Common\Slot;
12
13
use eZ\Publish\Core\SignalSlot\Signal;
14
use eZ\Publish\Core\Search\Common\Slot;
15
use eZ\Publish\SPI\Search\Indexing\ContentIndexing;
16
use eZ\Publish\SPI\Search\Indexing\FullTextIndexing;
17
18
/**
19
 * A Search Engine slot handling CopySubtreeSignal.
20
 */
21
class CopySubtree extends Slot
22
{
23
    /**
24
     * Receive the given $signal and react on it.
25
     *
26
     * @param \eZ\Publish\Core\SignalSlot\Signal $signal
27
     */
28
    public function receive(Signal $signal)
29
    {
30
        if (!$signal instanceof Signal\LocationService\CopySubtreeSignal || !$this->canIndex()) {
31
            return;
32
        }
33
34
        $contentHandler = $this->persistenceHandler->contentHandler();
35
        $subtreeIds = $this->persistenceHandler->locationHandler()->loadSubtreeIds($signal->targetNewSubtreeId);
36
37
        foreach ($subtreeIds as $contentId) {
38
            $contentInfo = $contentHandler->loadContentInfo($contentId);
39
40
            if ($this->canIndexContent()) {
41
                $this->searchHandler->indexContent(
42
                    $contentHandler->load($contentInfo->id, $contentInfo->currentVersionNo)
43
                );
44
            }
45
46
            if ($this->canIndexLocation()) {
47
                $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($contentInfo->id);
48
                foreach ($locations as $location) {
49
                    $this->searchHandler->indexLocation($location);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eZ\Publish\SPI\Search\Handler as the method indexLocation() does only exist in the following implementations of said interface: eZ\Publish\Core\Search\E...csearch\Content\Handler.

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...
50
                }
51
            }
52
        }
53
    }
54
55
    protected function canIndexContent()
56
    {
57
        return $this->searchHandler instanceof ContentIndexing || $this->searchHandler instanceof FullTextIndexing;
58
    }
59
}
60