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
|
|||
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 |
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: