Failed Conditions
Push — master ( 27ab61...b61ce7 )
by Yangsin
44:29
created

AbstractPluginGenerator::getHeader()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
nc 1
dl 0
loc 1
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23
 */
24
25
namespace Eccube\Command\PluginCommand;
26
27
use Eccube\Common\Constant;
28
use Symfony\Component\Console\Question\Question;
29
30
abstract class AbstractPluginGenerator
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
31
{
32
33
    const DEFAULT_NESTING_LEVEL = 100;
34
    const NEW_HOOK_VERSION = '3.0.9';
35
    const STOP_PROCESS = 'quit';
36
    const INPUT_OPEN = '[';
37
    const INPUT_CLOSE = ']';
38
39
    /**
40
     * app
41
     * @var \Eccube\Application
42
     */
43
    protected $app;
44
45
    /**
46
     * QuestionHelper
47
     * @var \Symfony\Component\Console\Helper\QuestionHelper
48
     */
49
    protected $dialog;
50
51
    /**
52
     * InputInterface
53
     * @var \Symfony\Component\Console\Input\InputInterface
54
     */
55
    protected $input;
56
57
    /**
58
     * InputInterface
59
     * @var \Symfony\Component\Console\Output\OutputInterface
60
     */
61
    protected $output;
62
63
    /**
64
     * $paramList
65
     * @var array $paramList
66
     */
67
    protected $paramList;
68
69
    /**
70
     *
71
     * @var int
72
     */
73
    private $nestingLevel;
74
75
    /**
76
     * ヘッダー
77
     */
78
    abstract protected function getHeader();
79
80
    /**
81
     * start()
82
     */
83
    abstract protected function start();
84
85
    /**
86
     * フィルドーセット
87
     */
88
    abstract protected function initFildset();
89
90
    public function __construct(\Eccube\Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
91
    {
92
        $this->app = $app;
93
        $this->nestingLevel = self::DEFAULT_NESTING_LEVEL;
94
    }
95
96
    /**
97
     * 
98
     * @param \Symfony\Component\Console\Helper\QuestionHelper $dialog
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
99
     * @param \Symfony\Component\Console\Input\InputInterface $input
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
100
     * @param \Symfony\Component\Console\Output\OutputInterface $output
101
     */
102
    public function init($dialog, $input, $output)
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
103
    {
104
        $this->dialog = $dialog;
105
        $this->input = $input;
106
        $this->output = $output;
107
        $this->initFildset();
108
    }
109
110
    public function run()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
111
    {
112
        //ヘッダー部分
113
        $this->getHeader();
114
115
        foreach ($this->paramList as $paramKey => $params) {
116
            $value = $this->makeLineRequest($params);
117
            if ($value === false) {
118
                $this->exitGenerator();
119
                return;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
120
            }
121
            $this->paramList[$paramKey]['value'] = $value;
122
        }
123
124
        $this->output->writeln('');
125
        $this->output->writeln('---Entry confirmation');
126
        foreach ($this->paramList as $paramKey => $params) {
127
            if (is_array($params['value'])) {
128
                $this->output->writeln($params['label']);
129
                foreach ($params['value'] as $keys => $val) {
130
                    $this->output->writeln('<info>  ' . $keys . '</info>');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
131
                }
132
            } else {
133
                if (isset($params['show'])) {
134
                    $disp = $params['show'][$params['value']];
135
                } else {
136
                    $disp = $params['value'];
137
                }
138
                $this->output->writeln($params['label'] . ' <info>' . $disp . '</info>');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
139
            }
140
        }
141
        $this->output->writeln('');
142
        $Question = new Question('<comment>[confirm] Do you want to proceed? [y/n] : </comment>', '');
143
        $value = $this->dialog->ask($this->input, $this->output, $Question);
144
        if ($value != 'y') {
145
            $this->exitGenerator();
146
            return;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
147
        }
148
149
        $this->start();
150
    }
151
152
    protected function exitGenerator($msg = 'Quitting Bye bye.')
153
    {
154
        $this->output->writeln($msg);
155
    }
156
157
    protected function makeLineRequest($params)
158
    {
159
        //nesting loop protection
160
        if ($this->getNestingLevel() < 0) {
161
            rewind($this->output->getStream());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method getStream() does only exist in the following implementations of said interface: Symfony\Component\Console\Output\ConsoleOutput, Symfony\Component\Console\Output\StreamOutput.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
162
            $display = stream_get_contents($this->output->getStream());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method getStream() does only exist in the following implementations of said interface: Symfony\Component\Console\Output\ConsoleOutput, Symfony\Component\Console\Output\StreamOutput.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
163
            throw new \Exception($display);
164
        }
165
        $this->nestingLevel--;
166
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
167
        $this->output->writeln($params['name']);
168
        $Question = new Question('<comment>Input' . self::INPUT_OPEN . $params['no'] . self::INPUT_CLOSE . ' : </comment>', '');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
169
        $value = $this->dialog->ask($this->input, $this->output, $Question);
170
        $value = trim($value);
171
        if ($value === self::STOP_PROCESS) {
172
            return false;
173
        }
174
        foreach ($params['validation'] as $key => $row) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
175
176
            if ($key == 'isRequired' && $row == true) {
177
                if ($value === '' || strlen($value) == 0) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
178
179
                    $this->output->writeln('[!] Value cannot be empty.');
180
                    return $this->makeLineRequest($params);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
181
                }
182
            } elseif ($key == 'patern' && preg_match($row, $value) == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match($row, $value) of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
183
                $this->output->writeln('<error>[!] Value is not valid.</error>');
184
                return $this->makeLineRequest($params);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
185
            } elseif ($key == 'inArray' || $key == 'choice') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
186
187
                if (is_string($row)) {
188
                    $row = $this->$row();
189
                }
190
                if ($value == '') {
191
                    return $params['value'];
192
                }
193
                if (isset($row[$value])) {
194
                    if (!is_array($params['value'])) {
195
                        $value = $row[$value];
196
                        continue;
197
                    }
198
                    $params['value'][$value] = $row[$value];
199
                    $this->output->writeln('<info>--- your entry list</info>');
200
                    foreach ($params['value'] as $subKey => $node) {
201
                        $this->output->writeln('<info> - ' . $subKey . '</info>');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
202
                    }
203
                    $this->output->writeln('');
204
                    $this->output->writeln('--- Press Enter to move to the next step ---');
205
206
                    return $this->makeLineRequest($params);
207
                } else {
208
                    $searchList = array();
209
                    $max = 16;
210
                    foreach ($row as $eventKey => $eventConst) {
211
                        if (strpos($eventKey, $value) !== false || strpos($eventConst, $value) !== false) {
212
                            if (count($searchList) >= $max) {
213
                                $searchList['-- there are more then ' . $max . ''] = '';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
214
                                break;
215
                            }
216
                            $searchList[$eventKey] = $eventConst;
217
                        }
218
                    }
219
                    $this->output->writeln('<error>[!] No results have been found</error>');
220
                    if (!empty($searchList)) {
221
                        $this->output->writeln('--- there are more then one search result');
222
                    }
223
                    foreach ($searchList as $subKey => $node) {
224
                        $this->output->writeln(' - ' . $subKey);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
225
                    }
226
227
                    if (!empty($searchList)) {
228
                        $this->output->writeln('');
229
                    }
230
                    return $this->makeLineRequest($params);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
231
                }
232
            }
233
        }
234
235
        return $value;
236
    }
237
238
    protected function getNestingLevel()
239
    {
240
        return $this->nestingLevel;
241
    }
242
243
    protected function setNestingLevel($nestingLevel)
244
    {
245
        $this->nestingLevel = $nestingLevel;
246
    }
247
}
248