From 2761732e28920a6bcc32339d213c957b149ed587 Mon Sep 17 00:00:00 2001 From: Michael Kopinsky Date: Thu, 21 Nov 2024 17:08:57 -0500 Subject: [PATCH] Fix phpstan error for `new static` without preventing subclassing https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static#add-%40phpstan-consistent-constructor-to-the-class For some reason the annotation isn't working for `Bruteforce` and `YearMatch` so I left those classes as final, at least for now --- phpstan-baseline.neon | 7 ------- phpstan.neon | 3 --- src/Matchers/Bruteforce.php | 6 ------ src/Matchers/DateMatch.php | 3 ++- src/Matchers/DictionaryMatch.php | 1 + src/Matchers/RepeatMatch.php | 3 ++- src/Matchers/SequenceMatch.php | 3 ++- src/Matchers/SpatialMatch.php | 3 ++- 8 files changed, 9 insertions(+), 20 deletions(-) delete mode 100644 phpstan-baseline.neon diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon deleted file mode 100644 index 7df3b82..0000000 --- a/phpstan-baseline.neon +++ /dev/null @@ -1,7 +0,0 @@ -parameters: - ignoreErrors: - - - message: '#^Unsafe usage of new static\(\)\.$#' - identifier: new.static - count: 1 - path: src/Matchers/DictionaryMatch.php diff --git a/phpstan.neon b/phpstan.neon index 2b8bf25..ac41719 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,3 @@ -includes: - - phpstan-baseline.neon - parameters: level: 0 paths: diff --git a/src/Matchers/Bruteforce.php b/src/Matchers/Bruteforce.php index 8e99092..3e08223 100644 --- a/src/Matchers/Bruteforce.php +++ b/src/Matchers/Bruteforce.php @@ -6,12 +6,6 @@ use ZxcvbnPhp\Scorer; -/** - * Class Bruteforce - * @package ZxcvbnPhp\Matchers - * - * Intentionally not named with Match suffix to prevent autoloading from Matcher. - */ final class Bruteforce extends BaseMatch { public const BRUTEFORCE_CARDINALITY = 10; diff --git a/src/Matchers/DateMatch.php b/src/Matchers/DateMatch.php index c67bffe..6db9cf9 100644 --- a/src/Matchers/DateMatch.php +++ b/src/Matchers/DateMatch.php @@ -6,7 +6,8 @@ use ZxcvbnPhp\Matcher; -final class DateMatch extends BaseMatch +/** @phpstan-consistent-constructor */ +class DateMatch extends BaseMatch { public const NUM_YEARS = 119; // Years match against 1900 - 2019 public const NUM_MONTHS = 12; diff --git a/src/Matchers/DictionaryMatch.php b/src/Matchers/DictionaryMatch.php index 334ac90..7be2340 100644 --- a/src/Matchers/DictionaryMatch.php +++ b/src/Matchers/DictionaryMatch.php @@ -7,6 +7,7 @@ use ZxcvbnPhp\Matcher; use ZxcvbnPhp\Math\Binomial; +/** @phpstan-consistent-constructor */ class DictionaryMatch extends BaseMatch { public $pattern = 'dictionary'; diff --git a/src/Matchers/RepeatMatch.php b/src/Matchers/RepeatMatch.php index fe44a03..1b23074 100644 --- a/src/Matchers/RepeatMatch.php +++ b/src/Matchers/RepeatMatch.php @@ -7,7 +7,8 @@ use ZxcvbnPhp\Matcher; use ZxcvbnPhp\Scorer; -final class RepeatMatch extends BaseMatch +/** @phpstan-consistent-constructor */ +class RepeatMatch extends BaseMatch { public const GREEDY_MATCH = '/(.+)\1+/u'; public const LAZY_MATCH = '/(.+?)\1+/u'; diff --git a/src/Matchers/SequenceMatch.php b/src/Matchers/SequenceMatch.php index 59531cf..17a8ea8 100644 --- a/src/Matchers/SequenceMatch.php +++ b/src/Matchers/SequenceMatch.php @@ -4,7 +4,8 @@ namespace ZxcvbnPhp\Matchers; -final class SequenceMatch extends BaseMatch +/** @phpstan-consistent-constructor */ +class SequenceMatch extends BaseMatch { public const MAX_DELTA = 5; diff --git a/src/Matchers/SpatialMatch.php b/src/Matchers/SpatialMatch.php index 32297e6..1a94603 100644 --- a/src/Matchers/SpatialMatch.php +++ b/src/Matchers/SpatialMatch.php @@ -7,7 +7,8 @@ use ZxcvbnPhp\Matcher; use ZxcvbnPhp\Math\Binomial; -final class SpatialMatch extends BaseMatch +/** @phpstan-consistent-constructor */ +class SpatialMatch extends BaseMatch { public const SHIFTED_CHARACTERS = '~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?';