GenerateEntity   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 93
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 4
dl 93
loc 93
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 17 17 1
A configureOptionals() 26 26 1
A execute() 30 30 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of Pomm's Cli package.
4
 *
5
 * (c) 2014 - 2015 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 PommProject\Foundation\ParameterHolder;
13
use PommProject\ModelManager\Generator\EntityGenerator;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * GenerateEntity
21
 *
22
 * Entity generation command.
23
 *
24
 * @package   Cli
25
 * @copyright 2014 - 2015 Grégoire HUBERT
26
 * @author    Grégoire HUBERT
27
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
28
 * @see       PommAwareCommand
29
 */
30 View Code Duplication
class GenerateEntity extends RelationAwareCommand
0 ignored issues
show
Duplication introduced by
This class 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...
31
{
32
    /**
33
     * configure
34
     *
35
     * @see Command
36
     */
37
    protected function configure()
38
    {
39
        $this
40
            ->setName('pomm:generate:entity')
41
            ->setDescription('Generate an Entity class.')
42
            ->setHelp(<<<HELP
43
This command generates an empty FlexibleEntity class in the given directory with the given namespace. By default, it creates a tree structure in the following format: ConfigName/NameSchema.
44
45
In order to comply with the project’s autoloading rules, it is possible to prefix this directory structure and / or namespace:
46
47
<info>pomm:generate:entity -d sources/lib/Model -a 'Vendor\Project\Model' --psr4 builder_name</info>
48
49
HELP
50
        )
51
        ;
52
        parent::configure();
53
    }
54
55
    /**
56
     * configureOptionals
57
     *
58
     * @see PommAwareCommand
59
     */
60
    protected function configureOptionals()
61
    {
62
        parent::configureOptionals()
63
            ->addOption(
64
                'force',
65
                null,
66
                InputOption::VALUE_NONE,
67
                'Force overwriting an existing file.'
68
            )
69
            ->addOption(
70
                'psr4',
71
                null,
72
                InputOption::VALUE_NONE,
73
                'Use PSR4 structure.'
74
            )
75
            ->addOption(
76
                'path-pattern',
77
                null,
78
                InputOption::VALUE_REQUIRED,
79
                'Use a different directory pattern when generating classes.',
80
                '{session}/{schema}Schema'
81
            )
82
        ;
83
84
        return $this;
85
    }
86
87
    /**
88
     * execute
89
     *
90
     * @see Command
91
     */
92
    protected function execute(InputInterface $input, OutputInterface $output)
93
    {
94
        parent::execute($input, $output);
95
96
        $session = $this->mustBeModelManagerSession($this->getSession());
97
98
        $this->pathFile = $this->getPathFile(
99
            $input->getArgument('config-name'),
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('config-name') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, PommProject\Cli\Command\...eCommand::getPathFile() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
100
            $this->relation,
0 ignored issues
show
Bug introduced by
It seems like $this->relation can also be of type array<integer,string> or null; however, PommProject\Cli\Command\...eCommand::getPathFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
101
            '',
102
            '',
103
            $input->getOption('psr4'),
104
            $input->getOption('path-pattern')
105
        );
106
        $this->namespace = $this->getNamespace($input->getArgument('config-name'), null, $input->getOption('path-pattern'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('config-name') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, PommProject\Cli\Command\...Command::getNamespace() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
107
108
        $this->updateOutput(
109
            $output,
110
            (new EntityGenerator(
111
                $session,
112
                $this->schema,
0 ignored issues
show
Bug introduced by
It seems like $this->schema can also be of type array<integer,string> or null; however, PommProject\ModelManager...enerator::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
113
                $this->relation,
0 ignored issues
show
Bug introduced by
It seems like $this->relation can also be of type array<integer,string> or null; however, PommProject\ModelManager...enerator::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
114
                $this->pathFile,
115
                $this->namespace,
116
                $this->flexible_container
117
            ))->generate(new ParameterHolder(['force' => $input->getOption('force')]))
118
        );
119
120
        return 0;
121
    }
122
}
123