GenerateForRelation::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 PommProject\ModelManager\Generator\ModelGenerator;
15
use PommProject\ModelManager\Generator\StructureGenerator;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * GenerateForRelation
23
 *
24
 * Generate a Structure, a model and an entity class if they do not already
25
 * exist (unless --force is specified).
26
 *
27
 * @package   Cli
28
 * @copyright 2014 - 2015 Grégoire HUBERT
29
 * @author    Grégoire HUBERT
30
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
31
 * @see       ModelGenerator
32
 */
33
class GenerateForRelation extends RelationAwareCommand
34
{
35
    /**
36
     * configure
37
     *
38
     * @see Command
39
     */
40
    public function configure()
41
    {
42
        $this
43
            ->setName('pomm:generate:relation-all')
44
            ->setDescription('Generate structure, model and entity file for a given relation.')
45
            ;
46
        parent::configure();
47
        $this
48
            ->addOption(
49
                'force',
50
                null,
51
                InputOption::VALUE_NONE,
52
                'Force overwriting existing files.'
53
            )
54
            ->addOption(
55
                'psr4',
56
                null,
57
                InputOption::VALUE_NONE,
58
                'Use PSR4 structure.'
59
            )
60
            ->addOption(
61
                'path-pattern',
62
                null,
63
                InputOption::VALUE_REQUIRED,
64
                'Use a different directory pattern when generating classes.',
65
                '{session}/{schema}Schema'
66
            )
67
        ;
68
    }
69
70
    /**
71
     * execute
72
     *
73
     * @see Command
74
     */
75
    protected function execute(InputInterface $input, OutputInterface $output)
76
    {
77
        parent::execute($input, $output);
78
79
        $session = $this->mustBeModelManagerSession($this->getSession());
80
81
        $this->updateOutput(
82
            $output,
83
            (new StructureGenerator(
84
                $session,
85
                $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...
86
                $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...
87
                $this->getPathFile($input->getArgument('config-name'), $this->relation, null, 'AutoStructure', $input->getOption('psr4'), $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\...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...
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...
88
                $this->getNamespace($input->getArgument('config-name'), 'AutoStructure', $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...
89
            ))->generate(new ParameterHolder(array_merge($input->getArguments(), $input->getOptions())))
90
        );
91
92
        $pathFile = $this->getPathFile($input->getArgument('config-name'), $this->relation, 'Model', '', $input->getOption('psr4'), $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\...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...
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...
93 View Code Duplication
        if (!file_exists($pathFile) || $input->getOption('force')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
94
            $this->updateOutput(
95
                $output,
96
                (new ModelGenerator(
97
                    $session,
98
                    $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...
99
                    $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...
100
                    $pathFile,
101
                    $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...
102
                ))->generate(new ParameterHolder(array_merge($input->getArguments(), $input->getOptions())))
103
            );
104
        } elseif ($output->isVerbose()) {
105
            $this->writelnSkipFile($output, $pathFile, 'model');
106
        }
107
108
        $pathFile = $this->getPathFile($input->getArgument('config-name'), $this->relation, '', '', $input->getOption('psr4'), $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\...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...
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...
109 View Code Duplication
        if (!file_exists($pathFile) || $input->getOption('force')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
110
            $this->updateOutput(
111
                $output,
112
                (new EntityGenerator(
113
                    $session,
114
                    $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...
115
                    $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...
116
                    $pathFile,
117
                    $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...
118
                    $this->flexible_container
119
                ))->generate(new ParameterHolder(array_merge($input->getArguments(), $input->getOptions())))
120
            );
121
        } elseif ($output->isVerbose()) {
122
            $this->writelnSkipFile($output, $pathFile, 'entity');
123
        }
124
125
        return 0;
126
    }
127
128
    /**
129
     * writelnSkipFile
130
     *
131
     * Write an informative message
132
     *
133
     * @access private
134
     * @param  OutputInterface $output
135
     * @param  string          $pathFile
136
     * @param  null|string     $file_type
137
     */
138
    private function writelnSkipFile(OutputInterface $output, $pathFile, $file_type = null)
139
    {
140
        $file_type = $file_type === null ? '' : sprintf("%s ", $file_type);
141
142
        $output->writeln(
143
            sprintf(
144
                " <fg=red>✗</fg=red>  <fg=blue>Preserving</fg=blue> existing %sfile <fg=yellow>'%s'</fg=yellow>.",
145
                $file_type,
146
                $pathFile
147
            )
148
        );
149
    }
150
}
151