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

ODM/MongoDB/Tools/Console/Command/QueryCommand.php (1 issue)

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;
6
7
use Doctrine\Common\Util\Debug;
8
use Symfony\Component\Console;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
use function is_numeric;
12
use function json_decode;
13
14
/**
15
 * Command to query mongodb and inspect the outputted results from your document classes.
16
 *
17
 */
18
class QueryCommand extends Console\Command\Command
19
{
20
    /**
21
     * @see Console\Command\Command
22
     */
23
    protected function configure()
24
    {
25
        $this
26
        ->setName('odm:query')
27
        ->setDescription('Query mongodb and inspect the outputted results from your document classes.')
28
        ->setDefinition([
29
            new InputArgument(
30
                'class',
31
                InputArgument::REQUIRED,
32
                'The class to query.'
33
            ),
34
            new InputArgument(
35
                'query',
36
                InputArgument::REQUIRED,
37
                'The query to execute and output the results for.'
38
            ),
39
            new InputOption(
40
                'hydrate',
41
                null,
42
                InputOption::VALUE_NONE,
43
                'Whether or not to hydrate the results in to document objects.'
44
            ),
45
            new InputOption(
46
                'skip',
47
                null,
48
                InputOption::VALUE_REQUIRED,
49
                'The number of documents to skip in the cursor.'
50
            ),
51
            new InputOption(
52
                'limit',
53
                null,
54
                InputOption::VALUE_REQUIRED,
55
                'The number of documents to return.'
56
            ),
57
            new InputOption(
58
                'depth',
59
                null,
60
                InputOption::VALUE_REQUIRED,
61
                'Dumping depth of Document graph.',
62
                7
63
            ),
64
        ])
65
        ->setHelp(<<<EOT
66
Execute a query and output the results.
67
EOT
68
        );
69
    }
70
71
    /**
72
     * @see Console\Command\Command
73
     */
74
    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
75
    {
76
        $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...
77
        $qb = $dm->getRepository($input->getArgument('class'))->createQueryBuilder();
78
        $qb->setQueryArray((array) json_decode($input->getArgument('query')));
79
        $qb->hydrate((bool) $input->getOption('hydrate'));
80
81
        $depth = $input->getOption('depth');
82
83
        if (! is_numeric($depth)) {
84
            throw new \LogicException("Option 'depth' must contain an integer value");
85
        }
86
87
        $skip = $input->getOption('skip');
88 View Code Duplication
        if ($skip !== null) {
89
            if (! is_numeric($skip)) {
90
                throw new \LogicException("Option 'skip' must contain an integer value");
91
            }
92
93
            $qb->skip((int) $skip);
94
        }
95
96
        $limit = $input->getOption('limit');
97 View Code Duplication
        if ($limit !== null) {
98
            if (! is_numeric($limit)) {
99
                throw new \LogicException("Option 'limit' must contain an integer value");
100
            }
101
102
            $qb->limit((int) $limit);
103
        }
104
105
        foreach ($qb->getQuery() as $result) {
106
            Debug::dump($result, $depth);
107
        }
108
    }
109
}
110