Completed
Push — master ( 7e29e1...6077fa )
by Pascal
02:32
created

ModelCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 17
cp 0
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A ModelCommand::execute() 0 4 1
1
<?php
2
namespace Atog\Api\Console;
3
4
use Symfony\Component\Console\Input\InputInterface;
5
use Symfony\Component\Console\Output\OutputInterface;
6
7
/**
8
 * Class ModelCommand
9
 * @package Atog\Console
10
 */
11
class ModelCommand extends AbstractScaffoldingCommand
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $type = 'model';
17
18
    /**
19
     * Execute command
20
     * @param \Symfony\Component\Console\Input\InputInterface   $input
21
     * @param \Symfony\Component\Console\Output\OutputInterface $output
22
     * @return void
23
     */
24
    protected function execute(InputInterface $input, OutputInterface $output)
25
    {
26
        $this->createFileTemplate($input, $output, $this->getApplication()->modelPath());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method modelPath() does only exist in the following sub-classes of Symfony\Component\Console\Application: Atog\Api\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
27
    }
28
29
    /**
30
     * File Template
31
     * @param string $namespace
32
     * @param string $name
33
     * @param string $topNSPart
34
     * @return string
35
     */
36
    protected function template($namespace, $name, $topNSPart = "Models")
37
    {
38
        $className = str_plural($name);
39
        $namespace = "{$namespace}\\{$topNSPart}";
40
41
        return <<< EOT
42
<?php
43
namespace $namespace;
44
45
use Atog\Api\Model;
46
47
/**
48
 * Class $className
49
 * @package $namespace
50
 */
51
class $className extends Model
52
{
53
}
54
55
EOT;
56
    }
57
}
58