Completed
Push — master ( 776a98...1eb6e4 )
by Jacob
11s
created

EmbedCollection::getHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace As3\Modlr\Models\Collections;
4
5
use As3\Modlr\Metadata\EmbedMetadata;
6
use As3\Modlr\Models\AbstractModel;
7
use As3\Modlr\Models\Embed;
8
use As3\Modlr\Store\Store;
9
10
/**
11
 * Model collection that contains embedded fragments from a persistence (database) layer.
12
 *
13
 * @author Jacob Bare <[email protected]>
14
 */
15
class EmbedCollection extends AbstractCollection
16
{
17
    /**
18
     * @var EmbedMetadata
19
     */
20
    protected $metadata;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param   EmbedMetadata  $metadata
26
     * @param   Store           $store
27
     * @param   AbstractModel[] $models
28
     */
29
    public function __construct(EmbedMetadata $metadata, Store $store, array $models = [])
30
    {
31
        $this->metadata = $metadata;
32
        parent::__construct($store, $models, count($models));
33
    }
34
35
    /**
36
     * Creates a new Embed model instance based on the collection
37
     *
38
     * @return  Embed
39
     */
40
    public function createNewEmbed()
41
    {
42
        $embed = $this->store->loadEmbed($this->getMetadata(), []);
43
        $embed->getState()->setNew();
44
        return $embed;
45
    }
46
47
    /**
48
     * Gets the unique hash for this collection.
49
     *
50
     * @return  string
51
     */
52
    public function getHash()
53
    {
54
        $hash = [];
55
        foreach ($this as $embed) {
56
            $hash[] = $embed->getHash();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class As3\Modlr\Models\AbstractModel as the method getHash() does only exist in the following sub-classes of As3\Modlr\Models\AbstractModel: As3\Modlr\Models\Embed. 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...
57
        }
58
        sort($hash);
59
        return md5(serialize($hash));
60
    }
61
62
    /**
63
     * Gets the metadata for the model collection.
64
     *
65
     * @return  EmbedMetadata
66
     */
67
    public function getMetadata()
68
    {
69
        return $this->metadata;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getType()
76
    {
77
        return $this->getMetadata()->name;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function isDirty()
84
    {
85
        if (true === $this->hasDirtyModels()) {
86
            return true;
87
        }
88
        return parent::isDirty();
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    protected function validateAdd(AbstractModel $model)
95
    {
96
        $this->validateModelClass($model);
97
        $this->store->validateEmbedSet($this->getMetadata(), $model->getName());
98
    }
99
100
    /**
101
     * Validates that the model class instance is supported.
102
     *
103
     * @param   AbstractModel   $model
104
     * @throws  \InvalidArgumentException
105
     */
106
    protected function validateModelClass(AbstractModel $model)
107
    {
108
        if (!$model instanceof Embed) {
109
            throw new \InvalidArgumentExcepton('The model must be an instanceof of Embed');
110
        }
111
    }
112
}
113