Completed
Push — master ( 5ea837...9dec3c )
by Paul
9s
created

CacheClearCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
A execute() 0 17 3
1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright  Copyright (c) 2011-2016 Paul Dragoonis <[email protected]>
6
 * @license    http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link       http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\Console\Command;
12
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * Clear the cache.
18
 *
19
 * @author Vítor Brandão <[email protected]>
20
 */
21
class CacheClearCommand extends AbstractCommand
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('cache:clear')
30
            ->setDescription('Clears the cache')
31
            ->setHelp(<<<EOF
32
The <info>%command.name%</info> command clears the application cache for a given environment
33
and debug mode:
34
35
<info>php %command.full_name% --env=dev</info>
36
<info>php %command.full_name% --env=prod --no-debug</info>
37
EOF
38
            );
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $cacheDir   = $this->getServiceManager()->getParameter('app.cache_dir');
47
        $filesystem = $this->getServiceManager()->getService('filesystem');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Depend...tion\ContainerInterface as the method getService() does only exist in the following implementations of said interface: PPI\Framework\ServiceManager\ServiceManager, PPI\Framework\ServiceManager\ServiceManagerBuilder.

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...
48
49
        if (!is_writable($cacheDir)) {
50
            throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $cacheDir));
51
        }
52
53
        $app = $this->getServiceManager()->get('app');
54
        $output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $app->getEnvironment(), var_export($app->isDebug(), true)));
55
        if (!$filesystem->exists($cacheDir)) {
56
            return;
57
        }
58
59
        $filesystem->remove(glob($cacheDir . '/*'));
60
    }
61
}
62