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

Console/Command/ClearCache/MetadataCommand.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\ClearCache;
6
7
use Doctrine\Common\Cache\ApcCache;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use const PHP_EOL;
12
13
/**
14
 * Command to clear the metadata cache of the various cache drivers.
15
 *
16
 */
17
class MetadataCommand extends Command
18
{
19
    /**
20
     * @see \Symfony\Component\Console\Command\Command
21
     */
22
    protected function configure()
23
    {
24
        $this
25
        ->setName('odm:clear-cache:metadata')
26
        ->setDescription('Clear all metadata cache of the various cache drivers.')
27
        ->setDefinition([])
28
        ->setHelp(<<<EOT
29
Clear all metadata cache of the various cache drivers.
30
EOT
31
        );
32
    }
33
34
    /**
35
     * @see \Symfony\Component\Console\Command\Command
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
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
        $cacheDriver = $dm->getConfiguration()->getMetadataCacheImpl();
41
42
        if (! $cacheDriver) {
43
            throw new \InvalidArgumentException('No Metadata cache driver is configured on given DocumentManager.');
44
        }
45
46
        if ($cacheDriver instanceof ApcCache) {
47
            throw new \LogicException('Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
48
        }
49
50
        $output->write('Clearing ALL Metadata cache entries' . PHP_EOL);
51
52
        $success = $cacheDriver->deleteAll();
53
54
        if ($success) {
55
            $output->write('The cache entries were successfully deleted.' . PHP_EOL);
56
        } else {
57
            $output->write('No entries to be deleted.' . PHP_EOL);
58
        }
59
    }
60
}
61