Skip to content

Commit

Permalink
Fix PHPStan level 8.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Oct 1, 2020
1 parent 5147d34 commit d1a2b04
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -50,7 +50,7 @@ script:
- if [[ $DEFAULT == 1 ]]; then vendor/bin/phpunit; fi
- if [[ $PREFER_LOWEST == 1 ]]; then vendor/bin/validate-prefer-lowest; fi

- if [[ $CHECKS == 1 ]]; then composer phpstan-setup && composer phpstan; fi
- if [[ $CHECKS == 1 ]]; then composer stan-setup && composer stan; fi
- if [[ $CHECKS == 1 ]]; then composer cs-check; fi

- if [[ $CODECOVERAGE == 1 ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=clover.xml; fi
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -23,7 +23,7 @@
},
"require-dev": {
"friendsofcake/search": "^6.0.0",
"dereuromark/cakephp-tools": "^2.0.0",
"dereuromark/cakephp-tools": "^2.1.3",
"dereuromark/cakephp-ide-helper": "^1.0.0",
"fig-r/psr2r-sniffer": "dev-master"
},
Expand All @@ -50,8 +50,8 @@
},
"prefer-stable": true,
"scripts": {
"phpstan": "phpstan analyse -c tests/phpstan.neon -l 5 src/",
"phpstan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.12 && mv composer.backup composer.json",
"stan": "phpstan analyse -c tests/phpstan.neon -l 8 src/",
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.12 && mv composer.backup composer.json",
"test": "php phpunit.phar",
"test-setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit-8.5.2.phar && mv phpunit-8.5.2.phar phpunit.phar || true",
"cs-check": "phpcs -p -s --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=/config/Migrations/ --extensions=php src/ tests/ config/",
Expand Down
15 changes: 9 additions & 6 deletions src/Controller/Admin/QueueController.php
Expand Up @@ -59,7 +59,7 @@ public function index() {
/**
* @param string|null $job
* @throws \Cake\Http\Exception\NotFoundException
* @return \Cake\Http\Response
* @return \Cake\Http\Response|null
*/
public function addJob($job = null) {
$this->request->allowMethod('post');
Expand All @@ -86,7 +86,7 @@ public function addJob($job = null) {
/**
* @param int|null $id
* @throws \Cake\Http\Exception\NotFoundException
* @return \Cake\Http\Response
* @return \Cake\Http\Response|null
*/
public function resetJob($id = null) {
$this->request->allowMethod('post');
Expand All @@ -104,7 +104,7 @@ public function resetJob($id = null) {
/**
* @param int|null $id
*
* @return \Cake\Http\Response
* @return \Cake\Http\Response|null
*/
public function removeJob($id = null) {
$this->request->allowMethod('post');
Expand Down Expand Up @@ -145,7 +145,7 @@ public function processes() {
/**
* Mark all failed jobs as ready for re-run.
*
* @return \Cake\Http\Response
* @return \Cake\Http\Response|null
*/
public function reset() {
$this->request->allowMethod('post');
Expand All @@ -160,7 +160,7 @@ public function reset() {
/**
* Truncate the queue list / table.
*
* @return \Cake\Http\Response
* @return \Cake\Http\Response|null
*/
public function hardReset() {
$this->request->allowMethod('post');
Expand All @@ -175,10 +175,13 @@ public function hardReset() {
/**
* @param string|array $default
*
* @return \Cake\Http\Response|null|void
* @return \Cake\Http\Response|null
*/
protected function refererRedirect($default) {
$url = $this->request->getQuery('redirect');
if (is_array($url)) {
throw new NotFoundException('Invalid array in query string');
}
if ($url && (mb_substr($url, 0, 1) !== '/' || mb_substr($url, 0, 2) === '//')) {
$url = null;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Controller/Admin/QueuedJobsController.php
Expand Up @@ -127,6 +127,9 @@ public function import() {
$file = $this->request->getData('file');
if ($file && $file['error'] == 0 && $file['size'] > 0) {
$content = file_get_contents($file['tmp_name']);
if ($content === false) {
throw new RuntimeException('Cannot parse file');
}
$json = json_decode($content, true);
if (empty($json) || empty($json['queuedJob'])) {
throw new RuntimeException('Invalid JSON content');
Expand Down
5 changes: 4 additions & 1 deletion src/Generator/Task/QueuedJobTask.php
Expand Up @@ -42,14 +42,17 @@ public function collect(): array {
/**
* @return string[]
*/
protected function collectQueuedJobTasks() {
protected function collectQueuedJobTasks(): array {
$result = [];

$taskFinder = new TaskFinder();
$tasks = $taskFinder->allAppAndPluginTasks();

foreach ($tasks as $task) {
$className = App::className($task, 'Shell/Task', 'Task');
if ($className === null) {
continue;
}
[, $task] = pluginSplit($task);
$task = substr($task, 5);
$result[$task] = $className;
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Table/QueueProcessesTable.php
Expand Up @@ -226,7 +226,7 @@ public function status() {
* @return string|null
*/
public function buildServerString() {
$serverName = env('SERVER_NAME') ?: gethostname();
$serverName = (string)env('SERVER_NAME') ?: gethostname();
if (!$serverName) {
$user = env('USER');
$logName = env('LOGNAME');
Expand Down
13 changes: 9 additions & 4 deletions src/Model/Table/QueuedJobsTable.php
Expand Up @@ -356,6 +356,10 @@ public function getFullStats($jobType = null) {
}

foreach ($result as $jobType => $jobs) {
/**
* @var string $day
* @var array $durations
*/
foreach ($jobs as $day => $durations) {
$average = array_sum($durations) / count($durations);
$result[$jobType][$day] = (int)$average;
Expand Down Expand Up @@ -521,6 +525,7 @@ public function requestJob(array $capabilities, array $groups = [], array $types

/** @var \Queue\Model\Entity\QueuedJob|null $job */
$job = $this->getConnection()->transactional(function () use ($query, $options, $now) {
/** @var \Queue\Model\Entity\QueuedJob|null $job */
$job = $query->find('all', $options)
->enableAutoFields(true)
->epilog('FOR UPDATE')
Expand Down Expand Up @@ -794,7 +799,7 @@ protected function _findProgress($state, $query = [], $results = []) {
*/
public function clearDoublettes() {
/** @var array $x */
$x = $this->_connection->query('SELECT max(id) as id FROM `' . $this->getTable() . '`
$x = $this->getConnection()->query('SELECT max(id) as id FROM `' . $this->getTable() . '`
WHERE completed is NULL
GROUP BY data
HAVING COUNT(id) > 1');
Expand Down Expand Up @@ -846,9 +851,9 @@ public function clearKey() {
public function truncate() {
/** @var \Cake\Database\Schema\TableSchema $schema */
$schema = $this->getSchema();
$sql = $schema->truncateSql($this->_connection);
$sql = $schema->truncateSql($this->getConnection());
foreach ($sql as $snippet) {
$this->_connection->execute($snippet);
$this->getConnection()->execute($snippet);
}
}

Expand Down Expand Up @@ -929,7 +934,7 @@ public function terminateProcess($pid, $sig = SIGTERM) {
*/
protected function _getDriverName() {
$className = explode('\\', $this->getConnection()->config()['driver']);
$name = end($className);
$name = end($className) ?: '';

return $name;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Queue/TaskFinder.php
Expand Up @@ -51,7 +51,7 @@ public function allAppAndPluginTasks() {
* @return string[]
*/
protected function getAppPaths(Folder $Folder) {
$res = array_merge($this->tasks, $Folder->find('Queue.+\.php'));
$res = array_merge((array)$this->tasks, $Folder->find('Queue.+\.php'));
foreach ($res as &$r) {
$r = basename($r, 'Task.php');
}
Expand All @@ -69,7 +69,7 @@ protected function getPluginPaths(Folder $Folder, $plugin) {
$res = $Folder->find('Queue.+Task\.php');
foreach ($res as $key => $r) {
$name = basename($r, 'Task.php');
if (in_array($name, $this->tasks)) {
if (in_array($name, (array)$this->tasks, true)) {
unset($res[$key]);

continue;
Expand Down
17 changes: 8 additions & 9 deletions src/Shell/BakeQueueTaskShell.php
Expand Up @@ -27,22 +27,21 @@ public function startup(): void {
* @return bool|int|null|void
*/
public function generate($name = null) {
$name = Inflector::camelize(Inflector::underscore($name));

$name = 'Queue' . $name . 'Task';
$plugin = $this->param('plugin') ?: null;
$name = Inflector::camelize(Inflector::underscore((string)$name));
$taskName = 'Queue' . $name . 'Task';
$plugin = (string)$this->param('plugin') ?: null;
if ($plugin) {
$plugin = Inflector::camelize(Inflector::underscore($plugin));
}

$this->generateTask($name, $plugin);
$this->generateTask($taskName, $plugin);

$this->generateTaskTest($name, $plugin);
$this->generateTaskTest($taskName, $plugin);
}

/**
* @param string $name
* @param string $plugin
* @param string|null $plugin
* @return void
*/
protected function generateTask($name, $plugin) {
Expand All @@ -52,7 +51,7 @@ protected function generateTask($name, $plugin) {
}

$path = array_shift($path);
if (!is_dir($path)) {
if ($path && !is_dir($path)) {
mkdir($path, 0770, true);
}

Expand All @@ -73,7 +72,7 @@ protected function generateTask($name, $plugin) {

/**
* @param string $name
* @param string $plugin
* @param string|null $plugin
* @return void
*/
protected function generateTaskTest($name, $plugin) {
Expand Down
10 changes: 6 additions & 4 deletions src/Shell/QueueShell.php
Expand Up @@ -187,8 +187,8 @@ public function runworker() {
$this->_exit = false;

$startTime = time();
$groups = $this->_stringToArray($this->param('group'));
$types = $this->_stringToArray($this->param('type'));
$groups = $this->_stringToArray((string)$this->param('group'));
$types = $this->_stringToArray((string)$this->param('type'));

while (!$this->_exit) {
$this->_setPhpTimeout();
Expand Down Expand Up @@ -260,7 +260,7 @@ protected function runJob(QueuedJob $queuedJob, $pid) {
try {
$this->_time = time();

$data = unserialize($queuedJob->data);
$data = $queuedJob->data ? unserialize($queuedJob->data) : null;
/** @var \Queue\Shell\Task\QueueTask $task */
$task = $this->{$taskName};
if (!$task instanceof QueueTaskInterface) {
Expand Down Expand Up @@ -622,7 +622,9 @@ protected function _logError($message, $pid = null) {
*/
protected function _getTaskConf() {
if (!is_array($this->_taskConf)) {
$this->_taskConf = Config::taskConfig($this->tasks);
/** @var array $tasks */
$tasks = $this->tasks;
$this->_taskConf = Config::taskConfig($tasks);
}

return $this->_taskConf;
Expand Down
13 changes: 1 addition & 12 deletions src/Shell/Task/QueueEmailTask.php
Expand Up @@ -104,7 +104,7 @@ public function run(array $data, int $jobId): void {

/** @var \Cake\Mailer\Mailer|null $mailer */
$mailer = $data['settings'];
if (is_object($mailer) && $mailer instanceof Mailer) {
if ($mailer && is_object($mailer) && $mailer instanceof Mailer) {
$this->mailer = $mailer;

$result = null;
Expand All @@ -131,17 +131,6 @@ public function run(array $data, int $jobId): void {
$this->mailer = $this->_getMailer();

$settings = $data['settings'] + $this->defaults;
$map = [
'to' => 'setTo',
'from' => 'setFrom',
'cc' => 'setCc',
'bcc' => 'setBcc',
'subject' => 'setSubject',
'sender' => 'setSender',
'replyTo' => 'setReplyTo',
'returnPath' => 'setReturnPath',
'readReceipt' => 'setReadReceipt',
];
foreach ($settings as $method => $setting) {
$setter = 'set' . ucfirst($method);
if (in_array($method, ['theme', 'template', 'layout'], true)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Shell/Task/QueueMonitorExampleTask.php
Expand Up @@ -82,7 +82,7 @@ protected function doMonitoring() {
* @return string[]
*/
protected function getSystemMemInfo() {
$data = explode("\n", file_get_contents('/proc/meminfo'));
$data = explode("\n", file_get_contents('/proc/meminfo') ?: '');
$meminfo = [];
foreach ($data as $line) {
if (strpos($line, ':') === false) {
Expand Down
6 changes: 5 additions & 1 deletion tests/TestCase/Shell/Task/QueueEmailTaskTest.php
Expand Up @@ -61,7 +61,11 @@ public function testRunArray() {
'to' => 'test@test.de',
];

$this->Task->run(['settings' => $settings, 'content' => 'Foo Bar'], 0);
$data = [
'settings' => $settings,
'content' => 'Foo Bar',
];
$this->Task->run($data, 0);

$this->assertInstanceOf(Mailer::class, $this->Task->mailer);

Expand Down
7 changes: 6 additions & 1 deletion tests/phpstan.neon
@@ -1,8 +1,13 @@
parameters:
autoload_files:
bootstrapFiles:
- bootstrap.php
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
earlyTerminatingMethodCalls:
Cake\Console\Shell:
- abort
ignoreErrors:
- '#Access to an undefined property Cake\\ORM\\BehaviorRegistry::\$Search#'
- '#Negated boolean expression is always false.#'
- '#Parameter \#1 \$function of function call_user_func_array expects .+, array\(.+\) given.#'
- '#Cannot cast array\|string\|null to .+.#'

0 comments on commit d1a2b04

Please sign in to comment.