From a5e9a132a97cdbe22d59ab2381cb28880a26e5c3 Mon Sep 17 00:00:00 2001 From: Arto Rozenga Date: Mon, 15 Mar 2021 15:16:02 +0100 Subject: [PATCH] Added phpstan and fixed issues till level 6 --- .travis.yml | 5 ++--- composer.json | 4 +++- phpstan.neon.dist | 4 ++++ src/Duration.php | 55 ++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 phpstan.neon.dist diff --git a/.travis.yml b/.travis.yml index 4358ef7..3c42620 100755 --- a/.travis.yml +++ b/.travis.yml @@ -7,14 +7,12 @@ sudo: false matrix: fast_finish: true include: - - php: 5.4 - - php: 5.5 - - php: 5.6 - php: 7.0 - php: 7.1 - php: 7.2 - php: 7.3 - php: 7.4 + - php: 8.0 branches: only: @@ -36,6 +34,7 @@ before_script: script: - composer run test + - composer run phpstan after_script: - php ./vendor/bin/coveralls diff --git a/composer.json b/composer.json index 2973003..e4e8c30 100755 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "php": ">=5.4" }, "require-dev": { + "phpstan/phpstan": "^0.12.81", "phpunit/phpunit": "~4.8|~5.0", "satooshi/php-coveralls": "~1.0", "symfony/yaml": "~2.0", @@ -38,7 +39,8 @@ } }, "scripts": { - "test": "phpunit -c phpunit.xml" + "test": "phpunit -c phpunit.xml", + "phpstan": "vendor/bin/phpstan" }, "minimum-stability": "stable" } diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..6e451fa --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,4 @@ +parameters: + level: 6 + paths: + - src diff --git a/src/Duration.php b/src/Duration.php index 0c6c0bf..82f5d73 100755 --- a/src/Duration.php +++ b/src/Duration.php @@ -4,23 +4,61 @@ class Duration { + /** + * @var int|float|null + */ public $days; + + /** + * @var int|float|null + */ public $hours; + + /** + * @var int|float|null + */ public $minutes; + + /** + * @var int|float|null + */ public $seconds; + /** + * @var int|null + */ public $hoursPerDay; + /** + * @var string|int + */ private $output; + + /** + * @var string + */ private $daysRegex; + + /** + * @var string + */ private $hoursRegex; + + /** + * @var string + */ private $minutesRegex; + + /** + * @var string + */ private $secondsRegex; /** * Duration constructor. * * @param int|float|string|null $duration + * @param int $hoursPerDay */ public function __construct($duration = null, $hoursPerDay = 24) { @@ -60,8 +98,8 @@ public function parse($duration) // count current precision $precision = 0; - if (($delimiterPos = strpos($this->seconds, '.')) !== false) { - $precision = strlen(substr($this->seconds, $delimiterPos + 1)); + if (($delimiterPos = strpos((string)$this->seconds, '.')) !== false) { + $precision = strlen(substr((string)$this->seconds, $delimiterPos + 1)); } $this->seconds = (float)round(($this->seconds - ($this->minutes * 60)), $precision); @@ -263,7 +301,11 @@ public function humanize($duration = null) } - private function numberBreakdown($number, $returnUnsigned = false) + /** + * @param float $number + * @return array|float[]|int[] + */ + private function numberBreakdown($number) { $negative = 1; @@ -272,13 +314,6 @@ private function numberBreakdown($number, $returnUnsigned = false) $number *= -1; } - if ($returnUnsigned) { - return array( - floor($number), - ($number - floor($number)) - ); - } - return array( floor($number) * $negative, ($number - floor($number)) * $negative