Completed
Push — master ( a10e7d...7e29e1 )
by Pascal
02:58
created

EndpointCommand::execute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 23
loc 23
ccs 0
cts 15
cp 0
rs 9.0856
cc 3
eloc 12
nc 2
nop 2
crap 12
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 EndpointCommand
11
 * @package Atog\Console
12
 */
13
class EndpointCommand extends Command
14
{
15
    /**
16
     * Command Config
17
     */
18
    protected function configure()
19
    {
20
        $this
21
            ->setName('create:endpoint')
22
            ->setDescription('Create a new Endpoint')
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 Endpoint'
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()->endpointPath();
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 endpointPath() 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 Endpoint: $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 $endpointNS
71
     * @return string
72
     */
73
    protected function template($namespace, $name, $endpointNS = "Endpoints")
74
    {
75
        $className = str_plural($name);
76
        $endpointName = strtolower($name);
77
        $namespace = "{$namespace}\\{$endpointNS}";
78
79
        return <<< EOT
80
<?php
81
namespace $namespace;
82
83
use Atog\Api\Endpoint;
84
85
/**
86
 * Class $className
87
 * @package $namespace
88
 */
89
class $className extends Endpoint
90
{
91
    /**
92
     * @var string
93
     */
94
    protected \$endpoint = '$endpointName';
95
}
96
97
EOT;
98
    }
99
}
100