Completed
Push — master ( 2f1eb7...23b3b5 )
by Maciej
10s
created

ValidateCommand::execute()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 19
cp 0
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 14
nc 12
nop 2
crap 30
1
<?php
2
3
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema;
4
5
use Doctrine\Common\Cache\VoidCache;
6
use Doctrine\ODM\MongoDB\DocumentManager;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class ValidateCommand extends Command
12
{
13
    /**
14
     * @see \Symfony\Component\Console\Command\Command
15
     */
16
    protected function configure()
17
    {
18
        $this
19
            ->setName('odm:schema:validate')
20
            ->setDescription('Validates if document mapping stays the same after serializing into cache.')
21
            ->setDefinition(array())
22
            ->setHelp(<<<EOT
23
Validates if document mapping stays the same after serializing into cache.
24
EOT
25
            );
26
    }
27
28
    /**
29
     * @see \Symfony\Component\Console\Command\Command
30
     */
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        /** @var DocumentManager $dm */
34
        $dm = $this->getHelper('documentManager')->getDocumentManager();
0 ignored issues
show
Bug introduced by
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...
35
        $metadataFactory = $dm->getMetadataFactory();
36
        $metadataFactory->setCacheDriver(new VoidCache());
37
38
        $errors = 0;
39
        foreach ($metadataFactory->getAllMetadata() as $meta) {
40
            if ($meta != unserialize(serialize($meta))) {
41
                ++$errors;
42
                $output->writeln(sprintf("%s has mapping issues.", $meta->name));
43
            }
44
        }
45
        if ($errors) {
46
            $output->writeln(sprintf('<error>%d document(s) have mapping issues.</error>'));
47
        } else {
48
            $output->writeln('All documents are OK!');
49
        }
50
51
        return $errors ? 255 : 0;
52
    }
53
}
54