1 | <?php |
||
20 | class MatchFormCreator extends ModelFormCreator |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | 1 | protected function build($builder) |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | * |
||
79 | * @param \Match $match |
||
80 | */ |
||
81 | public function fill($form, $match) |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | * |
||
104 | * @param \Match $match |
||
105 | */ |
||
106 | public function update($form, $match) |
||
107 | { |
||
108 | if (($match->getDuration() != $form->get('duration')->getData()) |
||
109 | || $match->getTimestamp()->ne($form->get('time')->getData())) { |
||
110 | // The timestamp of the match was changed, we might need to |
||
111 | // recalculate its ELO |
||
112 | $this->controller->recalculateNeeded = true; |
||
113 | } |
||
114 | |||
115 | $firstTeam = $form->get('first_team'); |
||
116 | $secondTeam = $form->get('second_team'); |
||
117 | |||
118 | if (!$match->isOfficial()) { |
||
119 | $match->setTeamColors( |
||
120 | $firstTeam->get('team')->getData(), |
||
121 | $secondTeam->get('team')->getData() |
||
122 | ); |
||
123 | } |
||
124 | |||
125 | $match->setTeamPlayers( |
||
126 | $this->getPlayerList($firstTeam), |
||
127 | $this->getPlayerList($secondTeam) |
||
128 | ); |
||
129 | |||
130 | $match->setTeamPoints( |
||
131 | $firstTeam->get('score')->getData(), |
||
132 | $secondTeam->get('score')->getData() |
||
133 | ); |
||
134 | |||
135 | $match->setDuration($form->get('duration')->getData()) |
||
136 | ->setServerAddress($form->get('server_address')->getData()) |
||
137 | ->setTimestamp($form->get('time')->getData()) |
||
138 | ->setMap($form->get('map')->getData()->getId()); |
||
139 | |||
140 | if (!$match->isEloCorrect()) { |
||
141 | $this->controller->recalculateNeeded = true; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 1 | public function enter($form) |
|
178 | |||
179 | /** |
||
180 | * Get the player list of a team |
||
181 | * |
||
182 | * @param FormInterface $team A MatchTeamType form |
||
183 | * @return array |
||
184 | */ |
||
185 | 1 | private function getPlayerList(FormInterface $team) |
|
189 | |||
190 | /** |
||
191 | * Get a function which converts models to their IDs |
||
192 | * |
||
193 | * Useful to store the match players into the database |
||
194 | */ |
||
195 | private static function getModelToID() |
||
201 | } |
||
202 |
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: