fucongcong/framework

View on GitHub
core/Group/Cron/ParseCrontab.php

Summary

Maintainability
A
3 hrs
Test Coverage
<?php
 
namespace Group\Cron;
 
class ParseCrontab
{
static public $error;
 
/**
* 解析crontab的定时格式,linux只支持到分钟/,这个类支持到秒
* @param string $crontab_string :
*
* 0 1 2 3 4 5
* * * * * * *
* - - - - - -
* | | | | | |
* | | | | | +----- day of week (0 - 6) (Sunday=0)
* | | | | +----- month (1 - 12)
* | | | +------- day of month (1 - 31)
* | | +--------- hour (0 - 23)
* | +----------- min (0 - 59)
* +------------- sec (0-59)
* @param int $start_time timestamp [default=current timestamp]
Line exceeds 120 characters; contains 135 characters
* @return int unix timestamp - 下一分钟内执行是否需要执行任务,如果需要,则把需要在那几秒执行返回
* @throws InvalidArgumentException 错误信息
*/
Method `parse` has 36 lines of code (exceeds 25 allowed). Consider refactoring.
Function `parse` has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
The method parse() has an NPath complexity of 252. The configured NPath complexity threshold is 200.
The method parse() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10.
The parameter $crontab_string is not named in camelCase.
The parameter $start_time is not named in camelCase.
The static declaration must come after the visibility declaration
The variable $crontab_string is not named in camelCase.
The variable $start_time is not named in camelCase.
static public function parse($crontab_string, $timezone = "PRC", $start_time = null)
Whitespace found at end of line
{
date_default_timezone_set($timezone);
 
Line exceeds 120 characters; contains 253 characters
if (!preg_match('/^((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)$/i', trim($crontab_string))) {
Line exceeds 120 characters; contains 224 characters
if (!preg_match('/^((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)$/i', trim($crontab_string))) {
self::$error = "Invalid cron string: " . $crontab_string;
return false;
}
}
if ($start_time && !is_numeric($start_time)) {
self::$error = "\$start_time must be a valid unix timestamp ($start_time given)";
return false;
}
$cron = preg_split("/[\s]+/i", trim($crontab_string));
$start = empty($start_time) ? time() : $start_time;
 
if (count($cron) == 5) {
$date = array(
'second' => [0],
'minutes' => self::_parse_cron_number($cron[0], 0, 59),
'hours' => self::_parse_cron_number($cron[1], 0, 23),
'day' => self::_parse_cron_number($cron[2], 1, 31),
'month' => self::_parse_cron_number($cron[3], 1, 12),
'week' => self::_parse_cron_number($cron[4], 0, 6),
);
$cron = \Cron\CronExpression::factory($cron[0].' '.$cron[1].' '.$cron[2].' '.$cron[3].' '.$cron[4].' *');
}
 
Consider simplifying this complex logical expression.
Expected 0 spaces after opening bracket; newline found
if (
in_array(intval(date('s', $start)), $date['second']) &&
in_array(intval(date('i', $start)), $date['minutes']) &&
in_array(intval(date('G', $start)), $date['hours']) &&
in_array(intval(date('j', $start)), $date['day']) &&
in_array(intval(date('w', $start)), $date['week']) &&
in_array(intval(date('n', $start)), $date['month'])
 
) {
//$preDate = $cron->getPreviousRunDate($nextruntime)->format('Y-m-d H:i:s');
$nextdate = $cron->getNextRunDate(date('Y-m-d H:i:s', $start))->format('Y-m-d H:i:s');
return strtotime($nextdate) - $start;
}
return null;
}
 
/**
* 解析单个配置的含义
* @param $s
* @param $min
* @param $max
* @return array
*/
Function `_parse_cron_number` has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
Avoid variables with short names like $s. Configured minimum length is 3.
Method name "_parse_cron_number" should not be prefixed with an underscore to indicate visibility
Method name "ParseCrontab::_parse_cron_number" is not in camel caps format
The static declaration must come after the visibility declaration
The variable $_min is not named in camelCase.
The variable $_max is not named in camelCase.
The method _parse_cron_number is not named in camelCase.
static protected function _parse_cron_number($s, $min, $max)
{
$result = array();
Avoid variables with short names like $v1. Configured minimum length is 3.
$v1 = explode(",", $s);
foreach ($v1 as $v2) {
Avoid variables with short names like $v3. Configured minimum length is 3.
$v3 = explode("/", $v2);
$step = empty($v3[1]) ? 1 : $v3[1];
Avoid variables with short names like $v4. Configured minimum length is 3.
$v4 = explode("-", $v3[0]);
$_min = count($v4) == 2 ? $v4[0] : ($v3[0] == "*" ? $min : $v3[0]);
$_max = count($v4) == 2 ? $v4[1] : ($v3[0] == "*" ? $max : $v3[0]);
for ($i = $_min; $i <= $_max; $i ++) {
if ($i % $step != 0) {
continue;
}
$result[$i] = intval($i);
}
}
ksort($result);
return $result;
}
Expected 1 newline at end of file; 0 found
}