SchemaAwareCommand::getNamespace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\Cli\Exception\CliException;
13
use PommProject\Foundation\Inflector;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * SchemaAwareCommand
22
 *
23
 * Base class for generator commands.
24
 *
25
 * @abstract
26
 * @package   Cli
27
 * @copyright 2014 - 2015 Grégoire HUBERT
28
 * @author    Grégoire HUBERT
29
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
30
 * @see       PommAwareCommand
31
 */
32
abstract class SchemaAwareCommand extends SessionAwareCommand
33
{
34
    protected $schema;
35
    protected $prefix_dir;
36
    protected $prefix_ns;
37
    protected $pathFile;
38
    protected $namespace;
39
    protected $flexible_container;
40
41
    /**
42
     * configure
43
     *
44
     * @see PommAwareCommand
45
     */
46
    protected function configureRequiredArguments()
47
    {
48
        parent::configureRequiredArguments()
49
            ->addOption(
50
                'prefix-dir',
51
                'd',
52
                InputOption::VALUE_REQUIRED,
53
                'Indicate a directory prefix.',
54
                '.'
55
            )
56
            ->addOption(
57
                'prefix-ns',
58
                'a',
59
                InputOption::VALUE_REQUIRED,
60
                'Indicate a namespace prefix.',
61
                ''
62
            )
63
        ;
64
65
        return $this;
66
    }
67
68
    /**
69
     * configureOptionals
70
     *
71
     * @see PommAwareCommand
72
     */
73
    protected function configureOptionals()
74
    {
75
        parent::configureOptionals()
76
            ->addArgument(
77
                'schema',
78
                InputArgument::OPTIONAL,
79
                'Schema of the relation.',
80
                'public'
81
            )
82
            ->addOption(
83
                'flexible-container',
84
                null,
85
                InputOption::VALUE_REQUIRED,
86
                'Use an alternative flexible entity container',
87
                'PommProject\ModelManager\Model\FlexibleEntity'
88
            )
89
        ;
90
91
        return $this;
92
    }
93
    /**
94
     * execute
95
     *
96
     * @see Command
97
     */
98
    protected function execute(InputInterface $input, OutputInterface $output)
99
    {
100
        parent::execute($input, $output);
101
        $this->schema   = $input->getArgument('schema');
102
103
        if (!$this->schema) {
104
            $this->schema = 'public';
105
        }
106
107
        $this->prefix_dir = $input->getOption('prefix-dir');
108
        $this->prefix_ns  = $input->getOption('prefix-ns');
109
        $this->flexible_container = $input->getOption('flexible-container');
110
    }
111
112
    /**
113
     * getPathFile
114
     *
115
     * Create path file from parameters and namespace.
116
     *
117
     * @access protected
118
     * @param  string $config_name
119
     * @param  string $file_suffix
120
     * @param  string $extra_dir
121
     * @param  string $file_name
122
     * @param  bool   $format_psr4
123
     * @return string
124
     */
125
    protected function getPathFile(
126
        $config_name,
0 ignored issues
show
Unused Code introduced by
The parameter $config_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
        $file_name,
128
        $file_suffix = '',
129
        $extra_dir = '',
130
        $format_psr4 = null,
131
        $path_pattern = '{session}/{schema}Schema'
132
    ) {
133
134
        $format_psr4 = $format_psr4 === null ? false : (bool) $format_psr4;
135
        $prefix_ns = "";
136
137
        if (!$format_psr4) {
138
            $prefix_ns = str_replace('\\', '/', trim($this->prefix_ns, '\\'));
139
        }
140
141
        $elements =
142
            [
143
                rtrim($this->prefix_dir, '/'),
144
                $prefix_ns,
145
                $this->expandPath($path_pattern),
146
                $extra_dir,
147
                sprintf("%s%s.php", Inflector::studlyCaps($file_name), $file_suffix)
148
            ];
149
150
        return join('/', array_filter($elements, function ($val) {
151
            return $val != null;
152
        }));
153
    }
154
155
    /**
156
     * expandPath
157
     *
158
     * Expand path pattern with the context values
159
     *
160
     * @param   string $pattern
161
     * @return  string
162
     */
163
    protected function expandPath($pattern)
164
    {
165
        return trim(
166
            strtr(
167
                $pattern,
168
                [
169
                    '{session}' => Inflector::studlyCaps($this->config_name),
0 ignored issues
show
Bug introduced by
It seems like $this->config_name can also be of type array<integer,string>; however, PommProject\Foundation\Inflector::studlyCaps() does only seem to accept string|null, 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...
170
                    '{schema}'  => Inflector::studlyCaps($this->schema),
0 ignored issues
show
Bug introduced by
It seems like $this->schema can also be of type array<integer,string>; however, PommProject\Foundation\Inflector::studlyCaps() does only seem to accept string|null, 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...
171
                ]
172
            ),
173
            '/'
174
        );
175
    }
176
177
    /**
178
     * getNamespace
179
     *
180
     * Create namespace from parameters.
181
     *
182
     * @access protected
183
     * @param  string $config_name
184
     * @param  string $extra_ns
185
     * @return string
186
     */
187
    protected function getNamespace($config_name, $extra_ns = '', $path_pattern = '{session}/{schema}Schema')
0 ignored issues
show
Unused Code introduced by
The parameter $config_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
188
    {
189
        $elements =
190
            [
191
                $this->prefix_ns,
192
                str_replace('/', '\\', $this->expandPath($path_pattern)),
193
                $extra_ns
194
            ];
195
196
        return join('\\', array_filter($elements, function ($val) {
197
            return $val != null;
198
        }));
199
    }
200
201
    /**
202
     * fetchSchemaOid
203
     *
204
     * Get the schema Oid from database.
205
     *
206
     * @access protected
207
     * @return int $oid
208
     * @throws CliException
209
     */
210
    protected function fetchSchemaOid()
211
    {
212
        $schema_oid = $this
213
            ->getSession()
214
            ->getInspector()
215
            ->getSchemaOid($this->schema)
216
            ;
217
218
        if ($schema_oid === null) {
219
            throw new CliException(
220
                sprintf(
221
                    "Could not find schema '%s'.",
222
                    $this->schema
223
                )
224
            );
225
        }
226
227
        return $schema_oid;
228
    }
229
}
230