php-rpc /
cruds
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ScayTrase\Api\Cruds; |
||
| 4 | |||
| 5 | use Doctrine\Common\Persistence\ManagerRegistry; |
||
| 6 | |||
| 7 | final class LoadingReferenceProvider implements ReferenceProviderInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var ManagerRegistry |
||
| 11 | */ |
||
| 12 | private $registry; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * PartialReferenceProvider constructor. |
||
| 16 | * |
||
| 17 | * @param ManagerRegistry $registry |
||
| 18 | */ |
||
| 19 | public function __construct(ManagerRegistry $registry) |
||
| 20 | { |
||
| 21 | $this->registry = $registry; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** {@inheritdoc} */ |
||
| 25 | public function getEntityReference($fqcn, $property, $identifier) |
||
| 26 | { |
||
| 27 | $metadata = $this->registry->getManagerForClass($fqcn)->getClassMetadata($fqcn); |
||
| 28 | |||
| 29 | if (!$metadata->hasAssociation($property)) { |
||
| 30 | return $identifier; |
||
| 31 | } |
||
| 32 | |||
| 33 | $target = $metadata->getAssociationTargetClass($property); |
||
| 34 | |||
| 35 | return $this->registry->getManagerForClass($fqcn)->getPartialReference($target, $identifier); |
||
|
0 ignored issues
–
show
|
|||
| 36 | } |
||
| 37 | } |
||
| 38 |
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: