Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

includes/Console/Command/Cache.php (2 issues)

Check for mismatching type of a variable.

Bug Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Console\Command;
3
4
use Redaxscript\Console\Parser;
5
use Redaxscript\Filesystem;
6
use function is_dir;
7
use function is_numeric;
8
use function is_object;
9
10
/**
11
 * children class to execute the cache command
12
 *
13
 * @since 3.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Console
17
 * @author Henry Ruhs
18
 */
19
20
class Cache extends CommandAbstract
21
{
22
	/**
23
	 * array of the command
24
	 *
25
	 * @var array
26
	 */
27
28
	protected $_commandArray =
29
	[
30
		'cache' =>
31
		[
32
			'description' => 'Cache command',
33
			'argumentArray' =>
34
			[
35
				'clear' =>
36
				[
37
					'description' => 'Clear the cache',
38
					'optionArray' =>
39
					[
40
						'directory' =>
41
						[
42
							'description' => 'Required directory of the cache'
43
						],
44
						'extension' =>
45
						[
46
							'description' => 'Required extension of the cache files'
47
						],
48
						'bundle' =>
49
						[
50
							'description' => 'Optional key or collection of the bundle'
51
						]
52
					]
53
				],
54
				'clear-invalid' =>
55
				[
56
					'description' => 'Clear the invalid cache',
57
					'optionArray' =>
58
					[
59
						'directory' =>
60
						[
61
							'description' => 'Required directory of the cache'
62
						],
63
						'extension' =>
64
						[
65
							'description' => 'Required extension of the cache files'
66
						],
67
						'lifetime' =>
68
						[
69
							'description' => 'Optional lifetime of the bundle'
70
						]
71
					]
72
				]
73
			]
74
		]
75
	];
76
77
	/**
78
	 * run the command
79
	 *
80
	 * @param string $mode name of the mode
81
	 *
82
	 * @since 3.0.0
83
	 *
84
	 * @return string|null
85
	 */
86
87 5
	public function run(string $mode = null) : ?string
88
	{
89 5
		$parser = new Parser($this->_request);
90 5
		$parser->init($mode);
91
92
		/* run command */
93
94 5
		$argumentKey = $parser->getArgument(1);
95 5
		$haltOnError = (bool)$parser->getOption('halt-on-error');
96 5
		if ($argumentKey === 'clear')
97
		{
98 2
			return $this->_clear($parser->getOption()) ? $this->success() : $this->error($haltOnError);
0 ignored issues
show
It seems like $parser->getOption() targeting Redaxscript\Console\Parser::getOption() can also be of type null or string; however, Redaxscript\Console\Command\Cache::_clear() does only seem to accept array, 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...
99
		}
100 3
		if ($argumentKey === 'clear-invalid')
101
		{
102 2
			return $this->_clearInvalid($parser->getOption()) ? $this->success() : $this->error($haltOnError);
0 ignored issues
show
It seems like $parser->getOption() targeting Redaxscript\Console\Parser::getOption() can also be of type null or string; however, Redaxscript\Console\Command\Cache::_clearInvalid() does only seem to accept array, 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...
103
		}
104 1
		return $this->getHelp();
105
	}
106
107
	/**
108
	 * clear the cache
109
	 *
110
	 * @since 3.0.0
111
	 *
112
	 * @param array $optionArray
113
	 *
114
	 * @return bool
115
	 */
116
117 2
	protected function _clear(array $optionArray = []) : bool
118
	{
119 2
		$directory = $this->prompt('directory', $optionArray);
120 2
		$extension = $this->prompt('extension', $optionArray);
121 2
		if (is_dir($directory))
122
		{
123 1
			$cacheFilesystem = new Filesystem\Cache();
124 1
			return is_object($cacheFilesystem->init($directory, $extension)->clear($optionArray['bundle']));
125
		}
126 1
		return false;
127
	}
128
129
	/**
130
	 * clear the invalid cache
131
	 *
132
	 * @since 3.0.0
133
	 *
134
	 * @param array $optionArray
135
	 *
136
	 * @return bool
137
	 */
138
139 2
	protected function _clearInvalid(array $optionArray = []) : bool
140
	{
141 2
		$directory = $this->prompt('directory', $optionArray);
142 2
		$extension = $this->prompt('extension', $optionArray);
143 2
		$lifetime = is_numeric($optionArray['lifetime']) ? $optionArray['lifetime'] : 3600;
144 2
		if (is_dir($directory))
145
		{
146 1
			$cacheFilesystem = new Filesystem\Cache();
147 1
			return is_object($cacheFilesystem->init($directory, $extension)->clearInvalid($lifetime));
148
		}
149 1
		return false;
150
	}
151
}
152