Completed
Push — master ( a8fe50...bce26f )
by Maciej
13s
created

Tools/Console/Command/Schema/ValidateCommand.php (2 issues)

Upgrade to new PHP Analysis Engine

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
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method getDocumentManager() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\Too...r\DocumentManagerHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
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