These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | |||
4 | namespace Kadet\Highlighter\bin\Commands\Test; |
||
5 | |||
6 | |||
7 | use Kadet\Highlighter\Formatter\FormatterInterface; |
||
8 | use Kadet\Highlighter\KeyLighter; |
||
9 | use Kadet\Highlighter\Language\Language; |
||
10 | use Kadet\Highlighter\Parser\Tokens; |
||
11 | use Kadet\Highlighter\Tests\Helpers\TestFormatter; |
||
12 | use Kadet\Highlighter\Utils\StringHelper; |
||
13 | use Symfony\Component\Console\Command\Command; |
||
14 | use Symfony\Component\Console\Input\InputArgument; |
||
15 | use Symfony\Component\Console\Input\InputInterface; |
||
16 | use Symfony\Component\Console\Input\InputOption; |
||
17 | use Symfony\Component\Console\Output\OutputInterface; |
||
18 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
||
19 | |||
20 | class RegenerateCommand extends Command |
||
21 | { |
||
22 | /** |
||
23 | * @var KeyLighter |
||
24 | */ |
||
25 | private $_keylighter; |
||
26 | /** @var \Kadet\Highlighter\Formatter\FormatterInterface */ |
||
27 | private $_formatter; |
||
28 | |||
29 | private $_input; |
||
30 | private $_output; |
||
31 | |||
32 | protected function configure() |
||
33 | { |
||
34 | $this->setName('test:regenerate') |
||
35 | ->addArgument('files', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'tests to regenerate', ['*']) |
||
36 | ->addOption('new', null, InputOption::VALUE_NONE, 'generate only new files') |
||
37 | ->addOption('review', 'r', InputOption::VALUE_OPTIONAL, 'review generated files') |
||
38 | ->setDescription('Regenerates test files') |
||
39 | ; |
||
40 | |||
41 | $this->_keylighter = KeyLighter::get(); |
||
42 | $this->_formatter = new TestFormatter(); |
||
43 | |||
44 | $this->_input = realpath(__DIR__.'/../../../Tests/Samples'); |
||
45 | $this->_output = realpath(__DIR__.'/../../../Tests/Expected/Test'); |
||
46 | } |
||
47 | |||
48 | protected function execute(InputInterface $input, OutputInterface $output) |
||
49 | { |
||
50 | $iterator = new \RecursiveIteratorIterator( |
||
51 | new \RecursiveDirectoryIterator( |
||
52 | $this->_input, |
||
53 | \RecursiveDirectoryIterator::SKIP_DOTS | \RecursiveDirectoryIterator::UNIX_PATHS |
||
54 | ), \RecursiveIteratorIterator::LEAVES_ONLY |
||
55 | ); |
||
56 | |||
57 | $reviewer = $this->_keylighter->getFormatter($input->getOption('review') ?: 'cli'); |
||
58 | |||
59 | /** @var \SplFileInfo $file */ |
||
60 | foreach ($iterator as $file) { |
||
61 | $pathname = substr($file->getPathname(), strlen($this->_input) + 1); |
||
62 | if(!$this->regenerate($input, $pathname)) { |
||
63 | continue; |
||
64 | } |
||
65 | |||
66 | $output->writeln("Generating $pathname...", OutputInterface::VERBOSITY_QUIET); |
||
67 | |||
68 | $language = Language::byFilename($pathname); |
||
69 | $tokens = $language->parse(file_get_contents($file->getPathname())); |
||
70 | |||
71 | if ($this->review($input, $output, $tokens, $reviewer)) { |
||
72 | $result = StringHelper::normalize($this->_formatter->format($tokens)); |
||
73 | |||
74 | if(!file_exists($this->_output.'/'.dirname($pathname))) { |
||
75 | mkdir($this->_output.'/'.dirname($pathname), true); |
||
76 | } |
||
77 | |||
78 | file_put_contents("{$this->_output}/$pathname.tkn", $result); |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | |||
83 | private function review(InputInterface $input, OutputInterface $output, Tokens $tokens, FormatterInterface $reviewer) |
||
84 | { |
||
85 | if (!$this->shouldReview($input)) { |
||
86 | return true; |
||
87 | } |
||
88 | |||
89 | $result = $reviewer->format($tokens); |
||
90 | $output->writeln($result); |
||
91 | |||
92 | $question = new ConfirmationQuestion('Does it look right?'); |
||
93 | return $this->getHelper('question')->ask($input, $output, $question); |
||
0 ignored issues
–
show
|
|||
94 | } |
||
95 | |||
96 | private function shouldReview(InputInterface $input) |
||
97 | { |
||
98 | return $input->hasParameterOption('--review') |
||
99 | || $input->hasParameterOption('-r'); |
||
100 | } |
||
101 | |||
102 | private function regenerate(InputInterface $input, $filename) { |
||
103 | $filename = str_replace(DIRECTORY_SEPARATOR, '/', $filename); |
||
104 | $patterns = $input->getArgument('files'); |
||
105 | |||
106 | foreach($patterns as $pattern) { |
||
107 | if($input->getOption('new') && file_exists("{$this->_output}/$filename.tkn")) { |
||
108 | continue; |
||
109 | } |
||
110 | |||
111 | if(fnmatch($pattern, $filename)) { |
||
112 | return true; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | return false; |
||
117 | } |
||
118 | } |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: