Browse Source

Fix PHPStan

mscherer 3 years ago
parent
commit
c583f99872

+ 2 - 2
src/Controller/Component/CommonComponent.php

@@ -263,10 +263,10 @@ class CommonComponent extends Component {
 
 			foreach ($this->controller->autoRedirectActions as $action) {
 				[$controller, $action] = pluginSplit($action);
-				if (!empty($controller) && $refererController !== '*' && $refererController !== $controller) {
+				if ($controller && $refererController !== '*' && $refererController !== $controller) {
 					continue;
 				}
-				if (empty($controller) && $refererController !== $this->controller->getRequest()->getParam('controller')) {
+				if (!$controller && $refererController !== $this->controller->getRequest()->getParam('controller')) {
 					continue;
 				}
 				if (!in_array($referer['action'], (array)$this->controller->autoRedirectActions, true)) {

+ 1 - 1
src/Controller/Component/MobileComponent.php

@@ -129,7 +129,7 @@ class MobileComponent extends Component {
 
 		if ($forceMobile !== null && !$forceMobile) {
 			$this->setMobile = false;
-		} elseif ($forceMobile !== null && $forceMobile || $this->isMobile()) {
+		} elseif ($forceMobile !== null || $this->isMobile()) {
 			$this->setMobile = true;
 		} else {
 			$this->setMobile = false;

+ 1 - 1
src/Mailer/Mailer.php

@@ -175,7 +175,7 @@ class Mailer extends CakeMailer {
 	/**
 	 * @return array
 	 */
-	public function getDebug(): array{
+	public function getDebug(): array {
 		return $this->debug;
 	}
 

+ 1 - 1
src/Mailer/MailerAwareTrait.php

@@ -38,7 +38,7 @@ trait MailerAwareTrait {
 	public function getMailer($name, $config = null) {
 		$className = App::className($name, 'Mailer', 'Mailer');
 
-		if (empty($className)) {
+		if (!$className) {
 			throw new MissingMailerException(compact('name'));
 		}
 

+ 2 - 2
src/Mailer/Message.php

@@ -153,7 +153,7 @@ class Message extends CakeMessage {
 	 * @return $this
 	 */
 	public function addEmbeddedAttachmentByContentId($contentId, $file, $name = null, array $options = []) {
-		if (empty($name)) {
+		if (!$name) {
 			$name = basename($file);
 		}
 		$name = pathinfo($name, PATHINFO_FILENAME) . '_' . md5($file) . '.' . pathinfo($name, PATHINFO_EXTENSION);
@@ -182,7 +182,7 @@ class Message extends CakeMessage {
 	 * @return string
 	 */
 	public function addEmbeddedAttachment(string $file, ?string $name = null, array $options = []): string {
-		if (empty($name)) {
+		if (!$name) {
 			$name = basename($file);
 		}
 

+ 2 - 3
src/Model/Behavior/BitmaskedBehavior.php

@@ -3,7 +3,6 @@
 namespace Tools\Model\Behavior;
 
 use ArrayObject;
-use Cake\Database\Expression\Comparison;
 use Cake\Database\Expression\ComparisonExpression;
 use Cake\Datasource\EntityInterface;
 use Cake\Event\EventInterface;
@@ -254,7 +253,7 @@ class BitmaskedBehavior extends Behavior {
 	 */
 	public function encodeBitmask($value, $defaultValue = null) {
 		$res = 0;
-		if (empty($value)) {
+		if (!$value) {
 			return $defaultValue;
 		}
 		foreach ((array)$value as $key => $val) {
@@ -284,7 +283,7 @@ class BitmaskedBehavior extends Behavior {
 		}
 
 		$callable = function ($comparison) use ($field, $mappedField) {
-			if (!$comparison instanceof Comparison && !$comparison instanceof ComparisonExpression) {
+			if (!$comparison instanceof ComparisonExpression) {
 				return $comparison;
 			}
 			$key = $comparison->getField();

+ 3 - 3
src/Model/Behavior/JsonableBehavior.php

@@ -4,7 +4,7 @@ namespace Tools\Model\Behavior;
 
 use ArrayObject;
 use Cake\Collection\CollectionInterface;
-use Cake\Database\Type;
+use Cake\Database\TypeFactory;
 use Cake\Datasource\EntityInterface;
 use Cake\Event\EventInterface;
 use Cake\ORM\Behavior;
@@ -98,7 +98,7 @@ class JsonableBehavior extends Behavior {
 			$this->_config['encodeParams']['options'] = $options;
 		}
 
-		Type::map('array', ArrayType::class);
+		TypeFactory::map('array', ArrayType::class);
 	}
 
 	/**
@@ -175,7 +175,7 @@ class JsonableBehavior extends Behavior {
 		$fields = [];
 
 		foreach ($mappedFields as $index => $map) {
-			if (empty($map) || $map == $usedFields[$index]) {
+			if (!$map || $map == $usedFields[$index]) {
 				$fields[$usedFields[$index]] = $usedFields[$index];
 
 				continue;

+ 18 - 21
src/Utility/FrozenTime.php

@@ -103,7 +103,7 @@ class FrozenTime extends CakeFrozenTime {
 	 * @return int Age (0 if both timestamps are equal or empty, -1 on invalid dates)
 	 */
 	public static function age($start, $end = null) {
-		if (empty($start) && empty($end) || $start == $end) {
+		if (!$start && $end || $start == $end) {
 			return 0;
 		}
 
@@ -273,14 +273,14 @@ class FrozenTime extends CakeFrozenTime {
 	public static function cWeekBeginning($year, $cWeek = 0) {
 		if ($cWeek <= 1 || $cWeek > static::cWeeks($year)) {
 			$first = mktime(0, 0, 0, 1, 1, $year);
-			$wtag = (int)date('w', $first);
+			$weekDay = (int)date('w', $first);
 
-			if ($wtag <= 4) {
+			if ($weekDay <= 4) {
 				/* Thursday or less: back to Monday */
-				$firstmonday = mktime(0, 0, 0, 1, 1 - ($wtag - 1), $year);
-			} elseif ($wtag != 1) {
+				$firstmonday = mktime(0, 0, 0, 1, 1 - ($weekDay - 1), $year);
+			} elseif ($weekDay !== 1) {
 				/* Back to Monday */
-				$firstmonday = mktime(0, 0, 0, 1, 1 + (7 - $wtag + 1), $year);
+				$firstmonday = mktime(0, 0, 0, 1, 1 + (7 - $weekDay + 1), $year);
 			} else {
 				$firstmonday = $first;
 			}
@@ -1059,24 +1059,21 @@ class FrozenTime extends CakeFrozenTime {
 		} else {
 			$explode = [$date];
 		}
-		if ($explode) {
-			$count = count($explode);
-			for ($i = 0; $i < $count; $i++) {
-				$explode[$i] = static::pad($explode[$i]);
-			}
-			$explode[0] = static::pad($explode[0], 4, '20');
 
-			if (count($explode) === 3) {
-				return implode('-', $explode) . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
-			}
-			if (count($explode) === 2) {
-				return implode('-', $explode) . '-' . ($type === 'end' ? static::daysInMonth((int)$explode[0], (int)$explode[1]) : '01') . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
-			}
+		$count = count($explode);
+		for ($i = 0; $i < $count; $i++) {
+			$explode[$i] = static::pad($explode[$i]);
+		}
+		$explode[0] = static::pad($explode[0], 4, '20');
 
-			return $explode[0] . '-' . ($type === 'end' ? '12' : '01') . '-' . ($type === 'end' ? '31' : '01') . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
+		if (count($explode) === 3) {
+			return implode('-', $explode) . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
+		}
+		if (count($explode) === 2) {
+			return implode('-', $explode) . '-' . ($type === 'end' ? static::daysInMonth((int)$explode[0], (int)$explode[1]) : '01') . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
 		}
 
-		return '';
+		return $explode[0] . '-' . ($type === 'end' ? '12' : '01') . '-' . ($type === 'end' ? '31' : '01') . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
 	}
 
 	/**
@@ -1204,7 +1201,7 @@ class FrozenTime extends CakeFrozenTime {
 	 * @return int Seconds
 	 */
 	public static function parseLocalTime($duration, array $allowed = [':', '.', ',']) {
-		if (empty($duration)) {
+		if (!$duration) {
 			return 0;
 		}
 		$parts = explode(' ', $duration);

+ 1 - 1
src/Utility/Number.php

@@ -184,7 +184,7 @@ class Number extends CakeNumber {
 	 * @return float Average
 	 */
 	public static function average($values, $precision = 0) {
-		if (empty($values)) {
+		if (!$values) {
 			return 0.0;
 		}
 

+ 1 - 1
src/Utility/Text.php

@@ -274,7 +274,7 @@ class Text extends CakeText {
 			$piece = str_replace($search, '', $piece);
 			$piece = trim($piece);
 
-			if (empty($piece) || !empty($options['min_char']) && mb_strlen($piece) < $options['min_char'] || !empty($options['max_char']) && mb_strlen($piece) > $options['max_char']) {
+			if (!$piece || !empty($options['min_char']) && mb_strlen($piece) < $options['min_char'] || !empty($options['max_char']) && mb_strlen($piece) > $options['max_char']) {
 				unset($pieces[$key]);
 			} else {
 				$pieces[$key] = $piece;

+ 18 - 21
src/Utility/Time.php

@@ -106,7 +106,7 @@ class Time extends CakeTime {
 	 * @return int Age (0 if both timestamps are equal or empty, -1 on invalid dates)
 	 */
 	public static function age($start, $end = null) {
-		if (empty($start) && empty($end) || $start == $end) {
+		if (!$start && !$end || $start == $end) {
 			return 0;
 		}
 
@@ -276,14 +276,14 @@ class Time extends CakeTime {
 	public static function cWeekBeginning($year, $cWeek = 0) {
 		if ($cWeek <= 1 || $cWeek > static::cWeeks($year)) {
 			$first = mktime(0, 0, 0, 1, 1, $year);
-			$wtag = (int)date('w', $first);
+			$weekDay = (int)date('w', $first);
 
-			if ($wtag <= 4) {
+			if ($weekDay <= 4) {
 				/* Thursday or less: back to Monday */
-				$firstmonday = mktime(0, 0, 0, 1, 1 - ($wtag - 1), $year);
-			} elseif ($wtag != 1) {
+				$firstmonday = mktime(0, 0, 0, 1, 1 - ($weekDay - 1), $year);
+			} elseif ($weekDay !== 1) {
 				/* Back to Monday */
-				$firstmonday = mktime(0, 0, 0, 1, 1 + (7 - $wtag + 1), $year);
+				$firstmonday = mktime(0, 0, 0, 1, 1 + (7 - $weekDay + 1), $year);
 			} else {
 				$firstmonday = $first;
 			}
@@ -1060,24 +1060,21 @@ class Time extends CakeTime {
 		} else {
 			$explode = [$date];
 		}
-		if ($explode) {
-			$count = count($explode);
-			for ($i = 0; $i < $count; $i++) {
-				$explode[$i] = static::pad($explode[$i]);
-			}
-			$explode[0] = static::pad($explode[0], 4, '20');
 
-			if (count($explode) === 3) {
-				return implode('-', $explode) . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
-			}
-			if (count($explode) === 2) {
-				return implode('-', $explode) . '-' . ($type === 'end' ? static::daysInMonth((int)$explode[0], (int)$explode[1]) : '01') . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
-			}
+		$count = count($explode);
+		for ($i = 0; $i < $count; $i++) {
+			$explode[$i] = static::pad($explode[$i]);
+		}
+		$explode[0] = static::pad($explode[0], 4, '20');
 
-			return $explode[0] . '-' . ($type === 'end' ? '12' : '01') . '-' . ($type === 'end' ? '31' : '01') . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
+		if (count($explode) === 3) {
+			return implode('-', $explode) . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
+		}
+		if (count($explode) === 2) {
+			return implode('-', $explode) . '-' . ($type === 'end' ? static::daysInMonth((int)$explode[0], (int)$explode[1]) : '01') . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
 		}
 
-		return '';
+		return $explode[0] . '-' . ($type === 'end' ? '12' : '01') . '-' . ($type === 'end' ? '31' : '01') . ' ' . ($type === 'end' ? '23:59:59' : '00:00:00');
 	}
 
 	/**
@@ -1205,7 +1202,7 @@ class Time extends CakeTime {
 	 * @return int Seconds
 	 */
 	public static function parseLocalTime($duration, array $allowed = [':', '.', ',']) {
-		if (empty($duration)) {
+		if (!$duration) {
 			return 0;
 		}
 		$parts = explode(' ', $duration);

+ 4 - 4
src/Utility/Utility.php

@@ -63,7 +63,7 @@ class Utility {
 			'clean' => true,
 		];
 		$options += $defaults;
-		if (empty($data)) {
+		if (!$data) {
 			return [];
 		}
 		$tokens = explode($separator, $data);
@@ -368,7 +368,7 @@ class Utility {
 	 * @return bool Result
 	 */
 	public static function logicalAnd($array) {
-		if (empty($array)) {
+		if (!$array) {
 			return false;
 		}
 		foreach ($array as $result) {
@@ -577,7 +577,7 @@ class Utility {
 		$path = null;
 
 		reset($data);
-		while (!empty($data)) {
+		while ($data) {
 			$key = key($data);
 			$element = $data[$key];
 			unset($data[$key]);
@@ -596,7 +596,7 @@ class Utility {
 				$result[] = $path . $element;
 			}
 
-			if (empty($data) && !empty($stack)) {
+			if (!$data && $stack) {
 				[$data, $path] = array_pop($stack);
 				reset($data);
 			}

+ 4 - 4
src/View/Helper/CommonHelper.php

@@ -96,11 +96,11 @@ class CommonHelper extends Helper {
 	 * Convenience method for clean meta name tags
 	 *
 	 * @param string|null $name Author, date, generator, revisit-after, language
-	 * @param array|string|null $content If array, it will be separated by commas
+	 * @param array<string>|string|null $content If array, it will be separated by commas
 	 * @return string HTML Markup
 	 */
 	public function metaName(?string $name = null, $content = null): string {
-		if (empty($name) || empty($content)) {
+		if (!$name || !$content) {
 			return '';
 		}
 
@@ -119,7 +119,7 @@ class CommonHelper extends Helper {
 	 * @return string HTML Markup
 	 */
 	public function metaDescription(string $content, ?string $language = null, array $options = []): string {
-		if (!empty($language)) {
+		if ($language) {
 			$options['lang'] = mb_strtolower($language);
 		} elseif ($language !== false) {
 			$options['lang'] = Configure::read('Config.locale');
@@ -214,7 +214,7 @@ class CommonHelper extends Helper {
 		$tags = [
 			'meta' => '<link rel="alternate" type="application/rss+xml" title="%s" href="%s"/>',
 		];
-		if (empty($title)) {
+		if (!$title) {
 			$title = __d('tools', 'Subscribe to this feed');
 		} else {
 			$title = h($title);

+ 1 - 1
src/View/Helper/FormatHelper.php

@@ -688,7 +688,7 @@ class FormatHelper extends Helper {
 		$options += $defaults;
 
 		// Sanity check
-		if (empty($array)) {
+		if (!$array) {
 			return '';
 		}
 

+ 1 - 1
src/View/Helper/NumberHelper.php

@@ -21,10 +21,10 @@ class NumberHelper extends CakeNumberHelper {
 	 *
 	 * @param \Cake\View\View $View The View this helper is being attached to.
 	 * @param array $config Configuration settings for the helper
-	 * @throws \Cake\Core\Exception\Exception When the engine class could not be found.
 	 */
 	public function __construct(View $View, array $config = []) {
 		$config = Hash::merge(['engine' => 'Tools.Number'], $config);
+
 		parent::__construct($View, $config);
 	}
 

+ 2 - 2
src/View/Helper/ObfuscateHelper.php

@@ -60,7 +60,7 @@ class ObfuscateHelper extends Helper {
 			'escape' => false,
 		];
 
-		if (empty($text)) {
+		if (!$text) {
 			$text = $this->encodeEmail($mail);
 		}
 
@@ -160,7 +160,7 @@ class ObfuscateHelper extends Helper {
 	 * @return string
 	 */
 	public function wordCensor($str, array $censored, $replacement = null) {
-		if (empty($censored)) {
+		if (!$censored) {
 			return $str;
 		}
 		$str = ' ' . $str . ' ';

+ 2 - 2
src/View/Helper/TextHelper.php

@@ -38,10 +38,10 @@ class TextHelper extends CakeTextHelper {
 	 *
 	 * @param \Cake\View\View $View the view object the helper is attached to.
 	 * @param array $config Settings array Settings array
-	 * @throws \Cake\Core\Exception\Exception when the engine class could not be found.
 	 */
 	public function __construct(View $View, array $config = []) {
 		$config = Hash::merge(['engine' => 'Tools.Text'], $config);
+
 		parent::__construct($View, $config);
 	}
 
@@ -56,7 +56,7 @@ class TextHelper extends CakeTextHelper {
 	 */
 	public function minimizeUrl($url, $max = null, array $options = []) {
 		// check if there is nothing to do
-		if (empty($url) || mb_strlen($url) <= (int)$max) {
+		if (!$url || mb_strlen($url) <= (int)$max) {
 			return $url;
 		}
 		// http:// etc has not to be displayed, so