| Conditions | 5 |
| Paths | 5 |
| Total Lines | 31 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 21 | public function validate($value, Constraint $constraint) |
||
| 22 | { |
||
| 23 | if (empty($value)) { |
||
| 24 | // No need to run any checks if no alias is provided |
||
| 25 | return; |
||
| 26 | } |
||
| 27 | |||
| 28 | $database = \Database::getInstance(); |
||
| 29 | $type = $constraint->type; |
||
| 30 | $table = $type::TABLE; |
||
| 31 | |||
| 32 | if ($constraint->model && $constraint->model->isValid()) { |
||
| 33 | // A model is being edited, make sure we don't show an error because |
||
| 34 | // its alias is found in the database |
||
| 35 | $results = $database->query( |
||
| 36 | "SELECT EXISTS(SELECT 1 FROM $table WHERE alias = ? AND id != ?) AS 'exists'", |
||
| 37 | 'si', |
||
| 38 | array($value, $constraint->model->getId()) |
||
| 39 | ); |
||
| 40 | } else { |
||
| 41 | $results = $database->query( |
||
| 42 | "SELECT EXISTS(SELECT 1 FROM $table WHERE alias = ?) AS 'exists'", |
||
| 43 | 's', |
||
| 44 | $value); |
||
| 45 | } |
||
| 46 | |||
| 47 | if ($results[0]['exists']) { |
||
| 48 | $this->context->buildViolation($constraint->message) |
||
|
|
|||
| 49 | ->addViolation(); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 |
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 implementation 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 interface: