1 | <?php |
||
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 = []) |
||
34 | |||
35 | /** |
||
36 | * Creates a new Embed model instance based on the collection |
||
37 | * |
||
38 | * @return Embed |
||
39 | */ |
||
40 | public function createNewEmbed() |
||
46 | |||
47 | /** |
||
48 | * Gets the unique hash for this collection. |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function getHash() |
||
61 | |||
62 | /** |
||
63 | * Gets the metadata for the model collection. |
||
64 | * |
||
65 | * @return EmbedMetadata |
||
66 | */ |
||
67 | public function getMetadata() |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function getType() |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function isDirty() |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | protected function validateAdd(AbstractModel $model) |
||
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) |
||
112 | } |
||
113 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: