Completed
Push — ezp-30882-thumbnail ( 1223cf...3134e5 )
by
unknown
15:29
created

FirstMatchingFieldStrategy::getThumbnail()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 2
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository\Strategy\ContentThumbnail;
10
11
use eZ\Publish\API\Repository\FieldTypeService;
12
use eZ\Publish\API\Repository\Values\Content\Field;
13
use eZ\Publish\API\Repository\Values\Content\Thumbnail;
14
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
15
use eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\Field\ThumbnailStrategy as ContentFieldThumbnailStrategy;
16
use eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\ThumbnailStrategy;
17
18
final class FirstMatchingFieldStrategy implements ThumbnailStrategy
19
{
20
    /** @var \eZ\Publish\API\Repository\FieldTypeService */
21
    private $fieldTypeService;
22
23
    /** @var \eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\Field\ThumbnailStrategy */
24
    private $contentFieldStrategy;
25
26
    public function __construct(
27
        ContentFieldThumbnailStrategy $contentFieldStrategy,
28
        FieldTypeService $fieldTypeService
29
    ) {
30
        $this->contentFieldStrategy = $contentFieldStrategy;
31
        $this->fieldTypeService = $fieldTypeService;
32
    }
33
34
    public function getThumbnail(ContentType $contentType, array $fields): ?Thumbnail
35
    {
36
        $fieldDefinitions = $contentType->getFieldDefinitions();
37
38
        foreach ($fieldDefinitions as $fieldDefinition) {
39
            $field = $this->getFieldByIdentifier($fieldDefinition->identifier, $fields);
40
41
            if ($field === null) {
42
                continue;
43
            }
44
45
            $fieldType = $this->fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier);
46
47
            if (
48
                $fieldDefinition->isThumbnail
49
                && $this->contentFieldStrategy->hasStrategy($field->fieldTypeIdentifier)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eZ\Publish\SPI\Repositor...Field\ThumbnailStrategy as the method hasStrategy() does only exist in the following implementations of said interface: eZ\Publish\Core\Reposito...ld\ContentFieldStrategy.

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
                && !$fieldType->isEmptyValue($field->value)
51
            ) {
52
                return $this->contentFieldStrategy->getThumbnail($field);
53
            }
54
        }
55
56
        return null;
57
    }
58
59
    private function getFieldByIdentifier(string $identifier, array $fields): ?Field
60
    {
61
        /** @var \eZ\Publish\API\Repository\Values\Content\Field $field */
62
        foreach ($fields as $field) {
63
            if ($field->fieldDefIdentifier === $identifier) {
64
                return $field;
65
            }
66
        }
67
68
        return null;
69
    }
70
}
71