Completed
Push — master ( 3abfec...c553e5 )
by Mikael
02:58
created

InspectType::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of Pomm's Cli package.
4
 *
5
 * (c) 2014 - 2017 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Cli\Command;
11
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * InspectType
18
 *
19
 * Display the list of converted types.
20
 *
21
 * @package   Cli
22
 * @copyright 2014 - 2015 Grégoire HUBERT
23
 * @author    Grégoire HUBERT
24
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
25
 * @see       SessionAwareCommand
26
 */
27
class InspectType extends SessionAwareCommand
28
{
29
    /**
30
     * configure
31
     *
32
     * @see command
33
     */
34
    protected function configure()
35
    {
36
        $this
37
            ->setName("pomm:inspect:type")
38
            ->setDescription("Show converted types list.")
39
            ;
40
41
        parent::configure();
42
    }
43
44
    /**
45
     * execute
46
     *
47
     * Set pomm dependent variables.
48
     *
49
     * @see Command
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        parent::execute($input, $output);
54
        $types = $this
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PommProject\Foundation\C...t\ClientPoolerInterface as the method getConverterHolder() does only exist in the following implementations of said interface: PommProject\Foundation\Converter\ConverterPooler.

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...
55
            ->getSession()
56
            ->getPoolerForType('converter')
57
            ->getConverterHolder()
58
            ->getTypes();
59
        $types = array_filter($types, function ($type) {
60
            return !preg_match('/^pg_catalog\./', $type);
61
        });
62
        natcasesort($types);
63
64
        foreach ($types as $type) {
65
                $output->writeln($type);
66
        }
67
    }
68
}
69