Browse Source

Fix phpstan

mscherer 5 years ago
parent
commit
9ddd8cde8d

+ 1 - 1
src/Controller/ShuntRequestController.php

@@ -25,7 +25,7 @@ class ShuntRequestController extends AppController {
 	/**
 	 * @var string|false
 	 */
-	public $modelClass = false;
+	protected $modelClass = false;
 
 	/**
 	 * @return void

+ 2 - 2
src/Shell/InflectShell.php

@@ -21,7 +21,7 @@ class InflectShell extends Shell {
 	 *
 	 * @var array
 	 */
-	public $validMethods = [
+	protected $validMethods = [
 		'pluralize', 'singularize', 'camelize',
 		'underscore', 'humanize', 'tableize',
 		'classify', 'variable', 'dasherize', 'slug',
@@ -32,7 +32,7 @@ class InflectShell extends Shell {
 	 *
 	 * @var array
 	 */
-	public $validCommands = [
+	protected $validCommands = [
 		'pluralize', 'singularize', 'camelize',
 		'underscore', 'humanize', 'tableize',
 		'classify', 'variable', 'dasherize', 'slug', 'all', 'quit',

+ 6 - 4
src/Utility/Time.php

@@ -94,8 +94,8 @@ class Time extends CakeTime {
 	 * Both dates default to current date. Note that start needs
 	 * to be before end for a valid result.
 	 *
-	 * @param int|string $start Start date (if empty, use today)
-	 * @param int|string|null $end End date (if empty, use today)
+	 * @param int|string|\DateTimeInterface $start Start date (if empty, use today)
+	 * @param int|string|\DateTimeInterface|null $end End date (if empty, use today)
 	 * @return int Age (0 if both timestamps are equal or empty, -1 on invalid dates)
 	 */
 	public static function age($start, $end = null) {
@@ -123,7 +123,9 @@ class Time extends CakeTime {
 		if ($startDate > $endDate) {
 			return -1;
 		}
+
 		$oDateInterval = $endDate->diff($startDate);
+
 		return $oDateInterval->y;
 	}
 
@@ -467,7 +469,7 @@ class Time extends CakeTime {
 	 * - default: Default string (defaults to "-----")
 	 * - oclock: Set to true to append oclock string
 	 *
-	 * @param string|null $dateString
+	 * @param string|\DateTimeInterface|null $dateString
 	 * @param string|null $format Format (YYYY-MM-DD, DD.MM.YYYY)
 	 * @param array $options Options
 	 * @return string
@@ -699,7 +701,7 @@ class Time extends CakeTime {
 
 	/**
 	 * @param int $offset in seconds
-	 * @param bool|null $past (defaults to null: return plain text)
+	 * @param bool|string|null $past (defaults to null: return plain text)
 	 * - new: if not boolean but a string use this as translating text
 	 * @return string text (i18n!)
 	 */

+ 26 - 22
src/View/Helper/CommonHelper.php

@@ -18,7 +18,7 @@ class CommonHelper extends Helper {
 	/**
 	 * @var array
 	 */
-	public $helpers = ['Html', 'Url'];
+	protected $helpers = ['Html', 'Url'];
 
 	/**
 	 * Auto-pluralizing a word using the Inflection class
@@ -30,13 +30,14 @@ class CommonHelper extends Helper {
 	 * @return string "member" or "members" OR "Mitglied"/"Mitglieder" if autoTranslate TRUE
 	 * @deprecated Use explicit form directly via sp()
 	 */
-	public function asp($singular, $count, $autoTranslate = false) {
-		if ((int)$count !== 1) {
-			$pural = Inflector::pluralize($singular);
+	public function asp(string $singular, int $count, bool $autoTranslate = false): string {
+		if ($count !== 1) {
+			$plural = Inflector::pluralize($singular);
 		} else {
-			$pural = null; // No pluralization necessary
+			$plural = ''; // No pluralization necessary
 		}
-		return $this->sp($singular, $pural, $count, $autoTranslate);
+
+		return $this->sp($singular, $plural, $count, $autoTranslate);
 	}
 
 	/**
@@ -49,8 +50,8 @@ class CommonHelper extends Helper {
 	 * @param bool $autoTranslate
 	 * @return string result
 	 */
-	public function sp($singular, $plural, $count, $autoTranslate = false) {
-		if ((int)$count !== 1) {
+	public function sp(string $singular, string $plural, int $count, bool $autoTranslate = false): string {
+		if ($count !== 1) {
 			$result = $plural;
 		} else {
 			$result = $singular;
@@ -59,6 +60,7 @@ class CommonHelper extends Helper {
 		if ($autoTranslate) {
 			$result = __($result);
 		}
+
 		return $result;
 	}
 
@@ -68,7 +70,7 @@ class CommonHelper extends Helper {
 	 * @param string|null $type - private/public
 	 * @return string HTML
 	 */
-	public function metaRobots($type = null) {
+	public function metaRobots(?string $type = null): string {
 		if ($type === null && ($meta = Configure::read('Config.robots')) !== null) {
 			$type = $meta;
 		}
@@ -92,13 +94,14 @@ class CommonHelper extends Helper {
 	 * @param string|array|null $content If array, it will be separated by commas
 	 * @return string HTML Markup
 	 */
-	public function metaName($name = null, $content = null) {
+	public function metaName(?string $name = null, $content = null): string {
 		if (empty($name) || empty($content)) {
 			return '';
 		}
 
 		$content = (array)$content;
 		$return = '<meta name="' . $name . '" content="' . implode(', ', $content) . '" />';
+
 		return $return;
 	}
 
@@ -110,12 +113,13 @@ class CommonHelper extends Helper {
 	 * @param array $options Additional options
 	 * @return string HTML Markup
 	 */
-	public function metaDescription($content, $language = null, $options = []) {
+	public function metaDescription(string $content, ?string $language = null, array $options = []): string {
 		if (!empty($language)) {
 			$options['lang'] = mb_strtolower($language);
 		} elseif ($language !== false) {
 			$options['lang'] = Configure::read('Config.locale');
 		}
+
 		return $this->Html->meta('description', $content, $options);
 	}
 
@@ -127,7 +131,7 @@ class CommonHelper extends Helper {
 	 * @param bool $escape
 	 * @return string HTML Markup
 	 */
-	public function metaKeywords($keywords = null, $language = null, $escape = true) {
+	public function metaKeywords($keywords = null, ?string $language = null, bool $escape = true): string {
 		if ($keywords === null) {
 			$keywords = Configure::read('Config.keywords');
 		}
@@ -143,6 +147,7 @@ class CommonHelper extends Helper {
 		} elseif ($language !== false) {
 			$options['lang'] = Configure::read('Config.locale');
 		}
+
 		return $this->Html->meta('keywords', $keywords, $options);
 	}
 
@@ -153,9 +158,10 @@ class CommonHelper extends Helper {
 	 * @param bool $full
 	 * @return string HTML Markup
 	 */
-	public function metaCanonical($url = null, $full = false) {
+	public function metaCanonical($url = null, bool $full = false): string {
 		$canonical = $this->Url->build($url, ['full' => $full]);
 		$options = ['rel' => 'canonical', 'link' => $canonical];
+
 		return $this->Html->meta($options);
 	}
 
@@ -171,7 +177,7 @@ class CommonHelper extends Helper {
 	 * @param bool $full
 	 * @return string HTML Markup
 	 */
-	public function metaAlternate($url, $lang, $full = false) {
+	public function metaAlternate($url, $lang, bool $full = false): string {
 		$url = $this->Url->build($url, ['full' => $full]);
 		$lang = (array)$lang;
 		$res = [];
@@ -188,6 +194,7 @@ class CommonHelper extends Helper {
 				$res[] = $this->Html->meta($options) . PHP_EOL;
 			}
 		}
+
 		return implode('', $res);
 	}
 
@@ -198,9 +205,9 @@ class CommonHelper extends Helper {
 	 * @param string|null $title
 	 * @return string HTML Markup
 	 */
-	public function metaRss($url, $title = null) {
+	public function metaRss($url, ?string $title = null): string {
 		$tags = [
-			'meta' => '<link rel="alternate" type="application/rss+xml" title="%s" href="%s" />',
+			'meta' => '<link rel="alternate" type="application/rss+xml" title="%s" href="%s"/>',
 		];
 		if (empty($title)) {
 			$title = __d('tools', 'Subscribe to this feed');
@@ -212,20 +219,17 @@ class CommonHelper extends Helper {
 	}
 
 	/**
-	 * Convenience method for META Tags
+	 * Convenience method for meta tags.
 	 *
 	 * @param string $type
 	 * @param string $value Content
 	 * @param bool $escape
 	 * @return string HTML Markup
 	 */
-	public function metaEquiv($type, $value, $escape = true) {
+	public function metaEquiv(string $type, string $value, bool $escape = true): string {
 		$tags = [
-			'meta' => '<meta http-equiv="%s"%s />',
+			'meta' => '<meta http-equiv="%s"%s/>',
 		];
-		if ($value === null) {
-			return '';
-		}
 		if ($escape) {
 			$value = h($value);
 		}

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

@@ -26,12 +26,12 @@ class FormatHelper extends Helper {
 	 *
 	 * @var array
 	 */
-	public $helpers = ['Html'];
+	protected $helpers = ['Html'];
 
 	/**
 	 * @var \Cake\View\StringTemplate
 	 */
-	public $template;
+	protected $template;
 
 	/**
 	 * @var array

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

@@ -60,7 +60,7 @@ class GravatarHelper extends Helper {
 	 *
 	 * @var array
 	 */
-	public $helpers = ['Html'];
+	protected $helpers = ['Html'];
 
 	/**
 	 * @param \Cake\View\View $View

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

@@ -28,7 +28,7 @@ class MeterHelper extends Helper {
 	/**
 	 * @var array
 	 */
-	public $helpers = ['Html'];
+	protected $helpers = ['Html'];
 
 	/**
 	 * @var array
@@ -81,7 +81,7 @@ class MeterHelper extends Helper {
 
 		$attributes += [
 			'value' => $value,
-			'min' => $min === null ? 0 : $min,
+			'min' => $min < 0 ? 0 : $min,
 			'max' => $max,
 			'title' => Number::toPercentage($progress, 0, ['multiply' => true]),
 		];

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

@@ -20,7 +20,7 @@ class ObfuscateHelper extends Helper {
 	 *
 	 * @var array
 	 */
-	public $helpers = ['Html'];
+	protected $helpers = ['Html'];
 
 	/**
 	 * It is still believed that encoding will stop spam-bots being able to find your email address.

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

@@ -29,7 +29,7 @@ class ProgressHelper extends Helper {
 	/**
 	 * @var array
 	 */
-	public $helpers = ['Html'];
+	protected $helpers = ['Html'];
 
 	/**
 	 * @var array

+ 6 - 6
src/View/Helper/QrCodeHelper.php

@@ -40,17 +40,17 @@ class QrCodeHelper extends Helper {
 	/**
 	 * @var array
 	 */
-	public $helpers = ['Html', 'Url'];
+	protected $helpers = ['Html', 'Url'];
 
 	/**
 	 * @var string
 	 */
-	public $engine = 'google';
+	protected $engine = 'google';
 
 	/**
 	 * @var string
 	 */
-	public $url = 'http://chart.apis.google.com/chart?';
+	protected $url = 'http://chart.apis.google.com/chart?';
 
 	/**
 	 * necessary
@@ -60,17 +60,17 @@ class QrCodeHelper extends Helper {
 	 *
 	 * @var array
 	 */
-	public $options = ['cht' => 'qr', 'chl' => '', 'choe' => '', 'chs' => ''];
+	protected $options = ['cht' => 'qr', 'chl' => '', 'choe' => '', 'chs' => ''];
 
 	/**
 	 * @var array
 	 */
-	public $ecLevels = ['H', 'Q', 'M', 'L']; # 30%..7%
+	protected $ecLevels = ['H', 'Q', 'M', 'L']; # 30%..7%
 
 	/**
 	 * @var array
 	 */
-	public $formattingTypes = ['url' => 'http', 'tel' => 'tel', 'sms' => 'smsto', 'card' => 'mecard'];
+	protected $formattingTypes = ['url' => 'http', 'tel' => 'tel', 'sms' => 'smsto', 'card' => 'mecard'];
 
 	/**
 	 * @param \Cake\View\View $View

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

@@ -19,7 +19,7 @@ class TimeHelper extends CakeTimeHelper {
 	/**
 	 * @var array
 	 */
-	public $helpers = ['Html'];
+	protected $helpers = ['Html'];
 
 	/**
 	 * Default config for this class

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

@@ -23,7 +23,7 @@ class TimelineHelper extends Helper {
 	/**
 	 * @var array
 	 */
-	public $helpers = ['Html'];
+	protected $helpers = ['Html'];
 
 	/**
 	 * Possible values are (with their default values):

+ 7 - 7
src/View/Helper/TypographyHelper.php

@@ -34,21 +34,21 @@ class TypographyHelper extends Helper {
 	 *
 	 * @var string
 	 */
-	public $blockElements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul';
+	protected $blockElements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul';
 
 	/**
 	 * Elements that should not have <p> and <br /> tags within them.
 	 *
 	 * @var string
 	 */
-	public $skipElements = 'p|pre|ol|ul|dl|object|table|h\d';
+	protected $skipElements = 'p|pre|ol|ul|dl|object|table|h\d';
 
 	/**
 	 * Tags we want the parser to completely ignore when splitting the string.
 	 *
 	 * @var string
 	 */
-	public $inlineElements =
+	protected $inlineElements =
 		'a|abbr|acronym|b|bdo|big|br|button|cite|code|del|dfn|em|i|img|ins|input|label|map|kbd|q|samp|select|small|span|strong|sub|sup|textarea|tt|var';
 
 	/**
@@ -56,26 +56,26 @@ class TypographyHelper extends Helper {
 	 *
 	 * @var array
 	 */
-	public $innerBlockRequired = ['blockquote'];
+	protected $innerBlockRequired = ['blockquote'];
 
 	/**
 	 * The last block element parsed
 	 *
 	 * @var string
 	 */
-	public $lastBlockElement = '';
+	protected $lastBlockElement = '';
 
 	/**
 	 * Whether or not to protect quotes within { curly braces }
 	 *
 	 * @var bool
 	 */
-	public $protectBracedQuotes = false;
+	protected $protectBracedQuotes = false;
 
 	/**
 	 * @var array
 	 */
-	public $matching = [
+	protected $matching = [
 		'deu' => 'low', // except for Switzerland
 		'eng' => 'default',
 		'fra' => 'angle',

+ 2 - 2
tests/TestCase/View/Helper/CommonHelperTest.php

@@ -83,7 +83,7 @@ class CommonHelperTest extends TestCase {
 	 */
 	public function testMetaRss() {
 		$result = $this->Common->metaRss('/some/url', 'some title');
-		$expected = '<link rel="alternate" type="application/rss+xml" title="some title" href="/some/url" />';
+		$expected = '<link rel="alternate" type="application/rss+xml" title="some title" href="/some/url"/>';
 		$this->assertEquals($expected, $result);
 	}
 
@@ -94,7 +94,7 @@ class CommonHelperTest extends TestCase {
 	 */
 	public function testMetaEquiv() {
 		$result = $this->Common->metaEquiv('type', 'value');
-		$expected = '<meta http-equiv="type" content="value" />';
+		$expected = '<meta http-equiv="type" content="value"/>';
 		$this->assertEquals($expected, $result);
 	}
 

+ 1 - 0
tests/phpstan.neon

@@ -7,6 +7,7 @@ parameters:
 		- %rootDir%/../../../src/Utility/Mime
 	ignoreErrors:
 		- '#Call to an undefined method .+TimeHelper::.+\(\)#'
+		- '#Comparison operation ">" between int<1, max> and 0 is always true.#'
 
 services:
 	-