Completed
Push — 1.x ( 2f995a...fa8c59 )
by Samuel
18:04
created

ApcBinLoadCommand::execute()   C

Complexity

Conditions 8
Paths 25

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 34
rs 5.3846
nc 25
cc 8
eloc 20
nop 2
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool\Command;
13
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class ApcBinLoadCommand extends AbstractCommand
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('apc:bin:load')
27
            ->setDescription('Load a binary dump into the APC file and user variables')
28
            ->addOption('--file', '-f', InputOption::VALUE_OPTIONAL, "File to read binary data from")
29
            ->addOption('--no-verification', '-a', InputOption::VALUE_NONE, "Don't perform MD5 & CRC32 verification before loading data")
30
            ->setHelp('');
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $this->ensureExtensionLoaded('apc');
39
40
        $file = $input->getOption('file');
41
        $noVerification = $input->getOption('no-verification');
42
43
        if (!$file) {
44
            $file = 'php://stdin';
45
        } else {
46
            if (!is_file($file) || !is_readable($file)) {
47
                throw new \InvalidArgumentException(sprintf("Could not read from file: %s", $file));
48
            }
49
        }
50
51
        $dump = file_get_contents($file);
52
        $flags = 0;
53
54
        if (!$noVerification) {
55
            $flags = APC_BIN_VERIFY_MD5 | APC_BIN_VERIFY_CRC32;
56
        }
57
58
        $success = $this->getCacheTool()->apc_bin_load($dump, $flags);
59
60
        if ($output->isVerbose()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method isVerbose() does only exist in the following implementations of said interface: Symfony\Component\Console\Output\BufferedOutput, Symfony\Component\Console\Output\ConsoleOutput, Symfony\Component\Console\Output\NullOutput, Symfony\Component\Console\Output\Output, Symfony\Component\Console\Output\StreamOutput, Symfony\Component\Consol...ts\Fixtures\DummyOutput, Symfony\Component\Console\Tests\Output\TestOutput.

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...
61
            if ($success) {
62
                $output->writeln("<comment>Load was successful</comment>");
63
            } else {
64
                $output->writeln("<comment>Load was unsuccessful</comment>");
65
            }
66
        }
67
68
        return $success ? 0 : 1;
69
    }
70
}
71