Skip to content

Commit

Permalink
Add PHPStan, fix phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mkopinsky committed Nov 20, 2024
1 parent 1ed4432 commit 69bb0d1
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 38 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require-dev": {
"phpunit/phpunit": "^8.5",
"php-coveralls/php-coveralls": "*",
"squizlabs/php_codesniffer": "3.*"
"squizlabs/php_codesniffer": "3.*",
"phpstan/phpstan": "^2.0"
},
"suggest": {
"ext-gmp": "Required for optimized binomial calculations (also requires PHP >= 7.3)"
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 0
paths:
- src
- test
5 changes: 1 addition & 4 deletions src/Matchers/BaseMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ZxcvbnPhp\Matchers;

use JetBrains\PhpStorm\ArrayShape;
use ZxcvbnPhp\Math\Binomial;
use ZxcvbnPhp\Scorer;

Expand Down Expand Up @@ -48,10 +47,8 @@ public function __construct(string $password, int $begin, int $end, string $toke
*
* @param bool $isSoleMatch
* Whether this is the only match in the password
* @return array
* Associative array with warning (string) and suggestions (array of strings)
* @return array{'warning': string, "suggestions": string[]}
*/
#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]
abstract public function getFeedback(bool $isSoleMatch): array;

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Matchers/Bruteforce.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ZxcvbnPhp\Matchers;

use JetBrains\PhpStorm\ArrayShape;
use ZxcvbnPhp\Scorer;

/**
Expand All @@ -13,7 +12,7 @@
*
* Intentionally not named with Match suffix to prevent autoloading from Matcher.
*/
class Bruteforce extends BaseMatch
final class Bruteforce extends BaseMatch
{
public const BRUTEFORCE_CARDINALITY = 10;

Expand All @@ -32,7 +31,9 @@ public static function match(string $password, array $userInputs = []): array
}


#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]
/**
* @return array{'warning': string, "suggestions": string[]}
*/
public function getFeedback(bool $isSoleMatch): array
{
return [
Expand Down
7 changes: 4 additions & 3 deletions src/Matchers/DateMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace ZxcvbnPhp\Matchers;

use JetBrains\PhpStorm\ArrayShape;
use ZxcvbnPhp\Matcher;

class DateMatch extends BaseMatch
final class DateMatch extends BaseMatch
{
public const NUM_YEARS = 119; // Years match against 1900 - 2019
public const NUM_MONTHS = 12;
Expand Down Expand Up @@ -108,7 +107,9 @@ public static function match(string $password, array $userInputs = []): array
return $matches;
}

#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]
/**
* @return array{'warning': string, "suggestions": string[]}
*/
public function getFeedback(bool $isSoleMatch): array
{
return [
Expand Down
7 changes: 2 additions & 5 deletions src/Matchers/DictionaryMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ZxcvbnPhp\Matchers;

use JetBrains\PhpStorm\ArrayShape;
use ZxcvbnPhp\Matcher;
use ZxcvbnPhp\Math\Binomial;

Expand Down Expand Up @@ -63,7 +62,7 @@ public static function match(string $password, array $userInputs = [], array $ra
$results = static::dictionaryMatch($password, $dict);
foreach ($results as $result) {
$result['dictionary_name'] = $name;
$matches[] = new static($password, $result['begin'], $result['end'], $result['token'], $result);
$matches[] = new DictionaryMatch($password, $result['begin'], $result['end'], $result['token'], $result);
}
}
Matcher::usortStable($matches, [Matcher::class, 'compareMatches']);
Expand All @@ -88,10 +87,8 @@ public function __construct(string $password, int $begin, int $end, string $toke
}

/**
* @param bool $isSoleMatch
* @return array
* @return array{'warning': string, "suggestions": string[]}
*/
#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]
public function getFeedback(bool $isSoleMatch): array
{
$startUpper = '/^[A-Z][^A-Z]+$/u';
Expand Down
5 changes: 3 additions & 2 deletions src/Matchers/L33tMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ZxcvbnPhp\Matchers;

use JetBrains\PhpStorm\ArrayShape;
use ZxcvbnPhp\Matcher;
use ZxcvbnPhp\Math\Binomial;

Expand Down Expand Up @@ -98,7 +97,9 @@ public function __construct(string $password, int $begin, int $end, string $toke
}
}

#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]
/**
* @return array{'warning': string, "suggestions": string[]}
*/
public function getFeedback(bool $isSoleMatch): array
{
$feedback = parent::getFeedback($isSoleMatch);
Expand Down
7 changes: 4 additions & 3 deletions src/Matchers/RepeatMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

namespace ZxcvbnPhp\Matchers;

use JetBrains\PhpStorm\ArrayShape;
use ZxcvbnPhp\Matcher;
use ZxcvbnPhp\Scorer;

class RepeatMatch extends BaseMatch
final class RepeatMatch extends BaseMatch
{
public const GREEDY_MATCH = '/(.+)\1+/u';
public const LAZY_MATCH = '/(.+?)\1+/u';
Expand Down Expand Up @@ -85,7 +84,9 @@ public static function match(string $password, array $userInputs = []): array
return $matches;
}

#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]
/**
* @return array{'warning': string, "suggestions": string[]}
*/
public function getFeedback(bool $isSoleMatch): array
{
$warning = mb_strlen($this->repeatedChar) == 1
Expand Down
5 changes: 3 additions & 2 deletions src/Matchers/ReverseDictionaryMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ZxcvbnPhp\Matchers;

use JetBrains\PhpStorm\ArrayShape;
use ZxcvbnPhp\Matcher;

class ReverseDictionaryMatch extends DictionaryMatch
Expand Down Expand Up @@ -42,7 +41,9 @@ protected function getRawGuesses(): float
return parent::getRawGuesses() * 2;
}

#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]
/**
* @return array{'warning': string, "suggestions": string[]}
*/
public function getFeedback(bool $isSoleMatch): array
{
$feedback = parent::getFeedback($isSoleMatch);
Expand Down
8 changes: 4 additions & 4 deletions src/Matchers/SequenceMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace ZxcvbnPhp\Matchers;

use JetBrains\PhpStorm\ArrayShape;

class SequenceMatch extends BaseMatch
final class SequenceMatch extends BaseMatch
{
public const MAX_DELTA = 5;

Expand Down Expand Up @@ -87,7 +85,9 @@ public static function findSequenceMatch(string $password, int $begin, int $end,
}
}

#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]
/**
* @return array{'warning': string, "suggestions": string[]}
*/
public function getFeedback(bool $isSoleMatch): array
{
return [
Expand Down
9 changes: 5 additions & 4 deletions src/Matchers/SpatialMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

namespace ZxcvbnPhp\Matchers;

use JetBrains\PhpStorm\ArrayShape;
use ZxcvbnPhp\Matcher;
use ZxcvbnPhp\Math\Binomial;

class SpatialMatch extends BaseMatch
final class SpatialMatch extends BaseMatch
{
public const SHIFTED_CHARACTERS = '~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?';

Expand Down Expand Up @@ -58,7 +57,9 @@ public static function match(string $password, array $userInputs = [], array $gr
return $matches;
}

#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]
/**
* @return array{'warning': string, "suggestions": string[]}
*/
public function getFeedback(bool $isSoleMatch): array
{
$warning = $this->turns == 1
Expand Down Expand Up @@ -93,7 +94,7 @@ public function __construct(string $password, int $begin, int $end, string $toke
/**
* Match spatial patterns in a adjacency graph.
* @param string $password
* @param array $graph
* @param array $graph
* @param string $graphName
* @return array
*/
Expand Down
8 changes: 5 additions & 3 deletions src/Matchers/YearMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace ZxcvbnPhp\Matchers;

use JetBrains\PhpStorm\ArrayShape;
use ZxcvbnPhp\Matcher;

class YearMatch extends BaseMatch
final class YearMatch extends BaseMatch
{
public const NUM_YEARS = 119;

Expand All @@ -32,7 +31,10 @@ public static function match(string $password, array $userInputs = []): array
return $matches;
}

#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]

/**
* @return array{'warning': string, "suggestions": string[]}
*/
public function getFeedback(bool $isSoleMatch): array
{
return [
Expand Down
5 changes: 1 addition & 4 deletions test/Matchers/MockMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ZxcvbnPhp\Test\Matchers;

use JetBrains\PhpStorm\ArrayShape;
use ZxcvbnPhp\Matchers\BaseMatch;

class MockMatch extends BaseMatch
Expand All @@ -22,10 +21,8 @@ public function __construct(int $begin, int $end, float $guesses)
* Get feedback to a user based on the match.
* @param bool $isSoleMatch
* Whether this is the only match in the password
* @return array
* Associative array with warning (string) and suggestions (array of strings)
* @return array{'warning': string, "suggestions": string[]}
*/
#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]
public function getFeedback(bool $isSoleMatch): array
{
return [
Expand Down

0 comments on commit 69bb0d1

Please sign in to comment.