These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema; |
||
6 | |||
7 | use Doctrine\Common\Cache\VoidCache; |
||
8 | use Doctrine\ODM\MongoDB\DocumentManager; |
||
9 | use Symfony\Component\Console\Command\Command; |
||
10 | use Symfony\Component\Console\Input\InputInterface; |
||
11 | use Symfony\Component\Console\Output\OutputInterface; |
||
12 | use function serialize; |
||
13 | use function sprintf; |
||
14 | use function unserialize; |
||
15 | |||
16 | class ValidateCommand extends Command |
||
17 | { |
||
18 | /** |
||
19 | * @see \Symfony\Component\Console\Command\Command |
||
20 | */ |
||
21 | protected function configure() |
||
22 | { |
||
23 | $this |
||
24 | ->setName('odm:schema:validate') |
||
25 | ->setDescription('Validates if document mapping stays the same after serializing into cache.') |
||
26 | ->setDefinition([]) |
||
27 | ->setHelp(<<<EOT |
||
28 | Validates if document mapping stays the same after serializing into cache. |
||
29 | EOT |
||
30 | ); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @see \Symfony\Component\Console\Command\Command |
||
35 | */ |
||
36 | protected function execute(InputInterface $input, OutputInterface $output) |
||
37 | { |
||
38 | /** @var DocumentManager $dm */ |
||
39 | $dm = $this->getHelper('documentManager')->getDocumentManager(); |
||
0 ignored issues
–
show
|
|||
40 | $metadataFactory = $dm->getMetadataFactory(); |
||
41 | $metadataFactory->setCacheDriver(new VoidCache()); |
||
42 | |||
43 | $errors = 0; |
||
44 | foreach ($metadataFactory->getAllMetadata() as $meta) { |
||
45 | if ($meta === unserialize(serialize($meta))) { |
||
46 | continue; |
||
47 | } |
||
48 | |||
49 | ++$errors; |
||
50 | $output->writeln(sprintf('%s has mapping issues.', $meta->name)); |
||
0 ignored issues
–
show
Accessing
name on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
51 | } |
||
52 | if ($errors) { |
||
53 | $output->writeln(sprintf('<error>%d document(s) have mapping issues.</error>', $errors)); |
||
54 | } else { |
||
55 | $output->writeln('All documents are OK!'); |
||
56 | } |
||
57 | |||
58 | return $errors ? 255 : 0; |
||
59 | } |
||
60 | } |
||
61 |
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: