From 19be8f510454fcdbcf74b67bdd3d9cfdab263834 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Sun, 26 May 2019 16:42:36 +0300 Subject: [PATCH] Fixed some phpstan level 2 issues --- system/src/Grav/Common/Helpers/Truncator.php | 20 ++++++++++--------- .../src/Grav/Common/Page/Medium/ImageFile.php | 2 +- system/src/Grav/Common/Twig/TwigExtension.php | 4 ++-- system/src/Grav/Common/Uri.php | 4 ++-- system/src/Grav/Common/Utils.php | 8 ++++---- .../Grav/Framework/Form/Traits/FormTrait.php | 10 +++++----- .../Psr7/Traits/UriDecorationTrait.php | 14 ++++++------- 7 files changed, 32 insertions(+), 30 deletions(-) diff --git a/system/src/Grav/Common/Helpers/Truncator.php b/system/src/Grav/Common/Helpers/Truncator.php index d116bb341..6827865d5 100644 --- a/system/src/Grav/Common/Helpers/Truncator.php +++ b/system/src/Grav/Common/Helpers/Truncator.php @@ -190,7 +190,7 @@ class Truncator { * Clean extra code * * @param DOMDocument $doc - * @param $container + * @param DOMDocument $container * @return string */ private static function getCleanedHTML(DOMDocument $doc, $container) @@ -203,8 +203,7 @@ class Truncator { $doc->appendChild($container->firstChild); } - $html = trim($doc->saveHTML()); - return $html; + return trim($doc->saveHTML()); } /** @@ -242,17 +241,20 @@ class Truncator { $ending = '...', $exact = false, $considerHtml = true - ) { + ) + { if ($considerHtml) { // if the plain text is shorter than the maximum length, return the whole text if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) { return $text; } + // splits all html-tags to scanable lines preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); $total_length = strlen($ending); - $open_tags = array(); $truncate = ''; + $open_tags = []; + foreach ($lines as $line_matchings) { // if there is any html-tag in this line, handle it and add it (uncounted) to the output if (!empty($line_matchings[1])) { @@ -308,22 +310,22 @@ class Truncator { } else { if (strlen($text) <= $length) { return $text; - } else { - $truncate = substr($text, 0, $length - strlen($ending)); } + + $truncate = substr($text, 0, $length - strlen($ending)); } // if the words shouldn't be cut in the middle... if (!$exact) { // ...search the last occurance of a space... $spacepos = strrpos($truncate, ' '); - if (isset($spacepos)) { + if (false !== $spacepos) { // ...and cut the text in this position $truncate = substr($truncate, 0, $spacepos); } } // add the defined ending to the text $truncate .= $ending; - if($considerHtml) { + if (isset($open_tags)) { // close all unclosed html-tags foreach ($open_tags as $tag) { $truncate .= ''; diff --git a/system/src/Grav/Common/Page/Medium/ImageFile.php b/system/src/Grav/Common/Page/Medium/ImageFile.php index d56c34220..82c61d216 100644 --- a/system/src/Grav/Common/Page/Medium/ImageFile.php +++ b/system/src/Grav/Common/Page/Medium/ImageFile.php @@ -109,7 +109,7 @@ class ImageFile extends Image * Gets the hash. * @param string $type * @param int $quality - * @param [] $extras + * @param array $extras * @return null */ public function getHash($type = 'guess', $quality = 80, $extras = []) diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index baced9abb..d66c905a9 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -455,9 +455,9 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn } /** - * Gets a human readable output for cron sytnax + * Gets a human readable output for cron syntax * - * @param $at + * @param string $at * @return string */ public function niceCronFilter($at) diff --git a/system/src/Grav/Common/Uri.php b/system/src/Grav/Common/Uri.php index 0bcba9a8c..0bb7aa9ea 100644 --- a/system/src/Grav/Common/Uri.php +++ b/system/src/Grav/Common/Uri.php @@ -1340,7 +1340,7 @@ class Uri /** * Check if this is a valid Grav extension * - * @param $extension + * @param string $extension * @return bool */ public function isValidExtension($extension) @@ -1357,7 +1357,7 @@ class Uri /** * Allow overriding of any element (be careful!) * - * @param $data + * @param array $data * @return Uri */ public function setUriProperties($data) diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 9b0992f99..194506c21 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -1406,16 +1406,16 @@ abstract class Utils /** * Parse a readable file size and return a value in bytes * - * @param string|int $size + * @param string|int|float $size * @return int */ public static function parseSize($size) { $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); - $size = preg_replace('/[^0-9\.]/', '', $size); + $size = (float)preg_replace('/[^0-9\.]/', '', $size); if ($unit) { - $size = $size * pow(1024, stripos('bkmgtpezy', $unit[0])); + $size *= 1024 ** stripos('bkmgtpezy', $unit[0]); } return (int) abs(round($size)); @@ -1502,7 +1502,7 @@ abstract class Utils } // Packed representation of IP - $ip = inet_pton($ip); + $ip = (string)inet_pton($ip); // Maximum netmask length = same as packed address $len = 8*strlen($ip); diff --git a/system/src/Grav/Framework/Form/Traits/FormTrait.php b/system/src/Grav/Framework/Form/Traits/FormTrait.php index 97348cd9d..cb253c54e 100644 --- a/system/src/Grav/Framework/Form/Traits/FormTrait.php +++ b/system/src/Grav/Framework/Form/Traits/FormTrait.php @@ -32,7 +32,7 @@ trait FormTrait { /** @var string */ public $status = 'success'; - /** @var string */ + /** @var string|null */ public $message; /** @var string[] */ public $messages = []; @@ -45,7 +45,7 @@ trait FormTrait private $uniqueid; /** @var bool */ private $submitted; - /** @var Data|object|null */ + /** @var \ArrayAccess|Data|object|null */ private $data; /** @var array|UploadedFileInterface[] */ private $files; @@ -485,11 +485,11 @@ trait FormTrait /** * Validate data and throw validation exceptions if validation fails. * - * @param \ArrayAccess $data + * @param \ArrayAccess|Data $data * @throws ValidationException * @throws \Exception */ - protected function validateData(\ArrayAccess $data): void + protected function validateData($data): void { if ($data instanceof Data) { $data->validate(); @@ -499,7 +499,7 @@ trait FormTrait /** * Filter validated data. * - * @param \ArrayAccess $data + * @param \ArrayAccess|Data $data */ protected function filterData(\ArrayAccess $data): void { diff --git a/system/src/Grav/Framework/Psr7/Traits/UriDecorationTrait.php b/system/src/Grav/Framework/Psr7/Traits/UriDecorationTrait.php index 9e7946a2a..9abb8b1be 100644 --- a/system/src/Grav/Framework/Psr7/Traits/UriDecorationTrait.php +++ b/system/src/Grav/Framework/Psr7/Traits/UriDecorationTrait.php @@ -65,64 +65,64 @@ trait UriDecorationTrait public function withScheme($scheme): UriInterface { - /** @var UriInterface|UriDecorationTrait $new */ $new = clone $this; $new->uri = $this->uri->withScheme($scheme); + /** @var UriInterface $new */ return $new; } public function withUserInfo($user, $password = null): UriInterface { - /** @var UriInterface|UriDecorationTrait $new */ $new = clone $this; $new->uri = $this->uri->withUserInfo($user, $password); + /** @var UriInterface $new */ return $new; } public function withHost($host): UriInterface { - /** @var UriInterface|UriDecorationTrait $new */ $new = clone $this; $new->uri = $this->uri->withHost($host); + /** @var UriInterface $new */ return $new; } public function withPort($port): UriInterface { - /** @var UriInterface|UriDecorationTrait $new */ $new = clone $this; $new->uri = $this->uri->withPort($port); + /** @var UriInterface $new */ return $new; } public function withPath($path): UriInterface { - /** @var UriInterface|UriDecorationTrait $new */ $new = clone $this; $new->uri = $this->uri->withPath($path); + /** @var UriInterface $new */ return $new; } public function withQuery($query): UriInterface { - /** @var UriInterface|UriDecorationTrait $new */ $new = clone $this; $new->uri = $this->uri->withQuery($query); + /** @var UriInterface $new */ return $new; } public function withFragment($fragment): UriInterface { - /** @var UriInterface|UriDecorationTrait $new */ $new = clone $this; $new->uri = $this->uri->withFragment($fragment); + /** @var UriInterface $new */ return $new; } }