Completed
Push — master ( a10e7d...7e29e1 )
by Pascal
02:58
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
<?php
2
namespace Atog\Api\Console;
3
4
use Symfony\Component\Console\Command\Command;
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Class ModelCommand
11
 * @package Atog\Console
12
 */
13
class ModelCommand extends Command
14
{
15
    /**
16
     * Command Config
17
     */
18
    protected function configure()
19
    {
20
        $this
21
            ->setName('create:model')
22
            ->setDescription('Create a new Model')
23
            ->addArgument(
24
                'namespace',
25
                InputArgument::REQUIRED,
26
                'Namespace in Dot-Notation e.g. Acme.Api'
27
            )
28
            ->addArgument(
29
                'name',
30
                InputArgument::REQUIRED,
31
                'Name of the Model'
32
            )
33
        ;
34
    }
35
36
    /**
37
     * Execute command
38
     * @param \Symfony\Component\Console\Input\InputInterface   $input
39
     * @param \Symfony\Component\Console\Output\OutputInterface $output
40
     * @return void
41
     */
42 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        // args
45
        $namespace = str_replace('.', '\\', $input->getArgument('namespace'));
46
        $name = $input->getArgument('name');
47
        $path = $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...
48
        $file = $path . str_plural($name) . '.php';
49
50
        // only write file if path exists and file does not exist yet
51
        if (file_exists($path) && !file_exists($file)) {
52
            // write file
53
            $endpoint = fopen($file, "w+");
54
            fwrite($endpoint, $this->template($namespace, $name));
55
            fclose($endpoint);
56
57
            // output
58
            $output->writeln("<info>Created Model: $name</info>");
59
            return;
60
        }
61
62
        // error
63
        $output->writeln("<error>Could not create file $file at $path</error>");
64
    }
65
66
    /**
67
     * File Template
68
     * @param string $namespace
69
     * @param string $name
70
     * @param string $modelNS
71
     * @return string
72
     */
73
    protected function template($namespace, $name, $modelNS = "Models")
74
    {
75
        $className = str_plural($name);
76
        $namespace = "{$namespace}\\{$modelNS}";
77
78
        return <<< EOT
79
<?php
80
namespace $namespace;
81
82
use Atog\Api\Model;
83
84
/**
85
 * Class $className
86
 * @package $namespace
87
 */
88
class $className extends Model
89
{
90
}
91
92
EOT;
93
    }
94
}
95