ModelCollection::allWithoutLoad()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace As3\Modlr\Models\Collections;
4
5
use As3\Modlr\Metadata\EntityMetadata;
6
use As3\Modlr\Models\AbstractModel;
7
use As3\Modlr\Models\Model;
8
use As3\Modlr\Store\Store;
9
10
/**
11
 * Model collection that contains record representations from a persistence (database) layer.
12
 *
13
 * @author Jacob Bare <[email protected]>
14
 */
15
abstract class ModelCollection extends AbstractCollection
16
{
17
    /**
18
     * @var EntityMetadata
19
     */
20
    protected $metadata;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param   EntityMetadata  $metadata
26
     * @param   Store           $store
27
     * @param   AbstractModel[] $models
28
     * @param   int             $totalCount
29
     */
30
    public function __construct(EntityMetadata $metadata, Store $store, array $models = [], $totalCount)
31
    {
32
        $this->metadata = $metadata;
33
        parent::__construct($store, $models, $totalCount);
34
    }
35
36
    /**
37
     * Returns all models in this collection without triggering auto-loading.
38
     *
39
     * @return  AbstractModel[]
40
     */
41
    public function allWithoutLoad()
42
    {
43
        return $this->models;
44
    }
45
46
    /**
47
     * Gets the identifiers for this collection.
48
     *
49
     * @param   bool    $onlyUnloaded   Whether to only include unloaded models in the results.
50
     * @return  array
51
     */
52
    abstract public function getIdentifiers($onlyUnloaded = true);
53
54
    /**
55
     * Gets the metadata for the model collection.
56
     *
57
     * @return  EntityMetadata
58
     */
59
    public function getMetadata()
60
    {
61
        return $this->metadata;
62
    }
63
64
    /**
65
     * Gets the query field for this collection.
66
     *
67
     * @return  string
68
     */
69
    abstract public function getQueryField();
70
71
    /**
72
     * {@inheritdoc}
73
     *
74
     * Overloaded to ensure models are loaded from the store.
75
     *
76
     */
77
    public function getSingleResult()
78
    {
79
        $this->loadFromStore();
80
        return parent::getSingleResult();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getType()
87
    {
88
        return $this->getMetadata()->type;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function rewind()
95
    {
96
        $this->loadFromStore();
97
        parent::rewind();
98
    }
99
100
    /**
101
     * Loads this collection from the store.
102
     */
103
    protected function loadFromStore()
104
    {
105
        if (false === $this->isLoaded()) {
106
            // Loads collection from the database on iteration.
107
            $models = $this->store->loadCollection($this);
108
            $this->setModels($models);
109
            $this->loaded = true;
110
        }
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    protected function validateAdd(AbstractModel $model)
117
    {
118
        $this->validateModelClass($model);
119
        $this->store->validateRelationshipSet($this->getMetadata(), $model->getType());
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 getType() does only exist in the following sub-classes of As3\Modlr\Models\AbstractModel: As3\Modlr\Models\Model. 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...
120
    }
121
122
    /**
123
     * Validates that the model class instance is supported.
124
     *
125
     * @param   AbstractModel   $model
126
     * @throws  \InvalidArgumentException
127
     */
128
    protected function validateModelClass(AbstractModel $model)
129
    {
130
        if (!$model instanceof Model) {
131
            throw new \InvalidArgumentException('The model must be an instanceof of Model');
132
        }
133
    }
134
}
135