Skip to content

Commit

Permalink
Merge pull request #50 from bjeavons/php8
Browse files Browse the repository at this point in the history
PHP compatibility
  • Loading branch information
mkopinsky authored Nov 30, 2020
2 parents f13e90c + 5de0063 commit 13b505d
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 66 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
- '7.2'
- '7.3'
- '7.4'
- '8.0'
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"php": "^7.2",
"php": "^7.2 | ^8.0",
"symfony/polyfill-mbstring": ">=1.3.1"
},
"require-dev": {
Expand Down
14 changes: 7 additions & 7 deletions src/Feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ZxcvbnPhp;

use ZxcvbnPhp\Matchers\Match;
use ZxcvbnPhp\Matchers\MatchInterface;

/**
* Feedback - gives some user guidance based on the strength
Expand All @@ -14,27 +14,27 @@ class Feedback
{
/**
* @param int $score
* @param Match[] $sequence
* @param MatchInterface[] $sequence
* @return array
*/
public function getFeedback($score, array $sequence)
{
// starting feedback
if (count($sequence) === 0) {
return [
'warning' => '',
'warning' => '',
'suggestions' => [
"Use a few words, avoid common phrases",
"No need for symbols, digits, or uppercase letters"
]
"No need for symbols, digits, or uppercase letters",
],
];
}

// no feedback if score is good or great.
if ($score > 2) {
return [
'warning' => '',
'suggestions' => []
'warning' => '',
'suggestions' => [],
];
}

Expand Down
16 changes: 8 additions & 8 deletions src/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ZxcvbnPhp;

use ZxcvbnPhp\Matchers\Match;
use ZxcvbnPhp\Matchers\BaseMatch;
use ZxcvbnPhp\Matchers\MatchInterface;

class Matcher
Expand All @@ -23,14 +23,14 @@ class Matcher
/**
* Get matches for a password.
*
* @see zxcvbn/src/matching.coffee::omnimatch
*
* @param string $password Password string to match
* @param array $userInputs Array of values related to the user (optional)
* @param string $password Password string to match
* @param array $userInputs Array of values related to the user (optional)
* @code array('Alice Smith')
* @endcode
*
* @return Match[] Array of Match objects.
* @return MatchInterface[] Array of Match objects.
*
* @see zxcvbn/src/matching.coffee::omnimatch
*/
public function getMatches($password, array $userInputs = [])
{
Expand Down Expand Up @@ -77,7 +77,7 @@ public static function usortStable(array &$array, callable $value_compare_func)
{
$index = 0;
foreach ($array as &$item) {
$item = array($index++, $item);
$item = [$index++, $item];
}
$result = usort($array, function ($a, $b) use ($value_compare_func) {
$result = $value_compare_func($a[1], $b[1]);
Expand All @@ -89,7 +89,7 @@ public static function usortStable(array &$array, callable $value_compare_func)
return $result;
}

public static function compareMatches(Match $a, Match $b)
public static function compareMatches(BaseMatch $a, BaseMatch $b)
{
$beginDiff = $a->begin - $b->begin;
if ($beginDiff) {
Expand Down
2 changes: 1 addition & 1 deletion src/Matchers/Match.php → src/Matchers/BaseMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use ZxcvbnPhp\Scorer;

abstract class Match implements MatchInterface
abstract class BaseMatch implements MatchInterface
{

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Matchers/Bruteforce.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* Intentionally not named with Match suffix to prevent autoloading from Matcher.
*/
class Bruteforce extends Match
class Bruteforce extends BaseMatch
{

public const BRUTEFORCE_CARDINALITY = 10;
Expand Down
2 changes: 1 addition & 1 deletion src/Matchers/DateMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use ZxcvbnPhp\Matcher;

class DateMatch extends Match
class DateMatch extends BaseMatch
{
public const NUM_YEARS = 119; // Years match against 1900 - 2019
public const NUM_MONTHS = 12;
Expand Down
2 changes: 1 addition & 1 deletion src/Matchers/DictionaryMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use ZxcvbnPhp\Matcher;

class DictionaryMatch extends Match
class DictionaryMatch extends BaseMatch
{

public $pattern = 'dictionary';
Expand Down
16 changes: 8 additions & 8 deletions src/Matchers/RepeatMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
use ZxcvbnPhp\Matcher;
use ZxcvbnPhp\Scorer;

class RepeatMatch extends Match
class RepeatMatch extends BaseMatch
{
public const GREEDY_MATCH = '/(.+)\1+/u';
public const LAZY_MATCH = '/(.+?)\1+/u';
public const ANCHORED_LAZY_MATCH = '/^(.+?)\1+$/u';

public $pattern = 'repeat';

/** @var Match[] An array of matches for the repeated section itself. */
/** @var MatchInterface[] An array of matches for the repeated section itself. */
public $baseMatches = [];

/** @var int The number of guesses required for the repeated section itself. */
Expand Down Expand Up @@ -70,9 +70,9 @@ public static function match($password, array $userInputs = [])
$match[0]['token'],
[
'repeated_char' => $repeatedChar,
'base_guesses' => $baseGuesses,
'base_matches' => $baseMatches,
'repeat_count' => $repeatCount
'base_guesses' => $baseGuesses,
'base_matches' => $baseMatches,
'repeat_count' => $repeatCount,
]
);

Expand All @@ -89,10 +89,10 @@ public function getFeedback($isSoleMatch)
: 'Repeats like "abcabcabc" are only slightly harder to guess than "abc"';

return [
'warning' => $warning,
'warning' => $warning,
'suggestions' => [
'Avoid repeated words and characters'
]
'Avoid repeated words and characters',
],
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Matchers/SequenceMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ZxcvbnPhp\Matchers;

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

Expand Down
2 changes: 1 addition & 1 deletion src/Matchers/SpatialMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use ZxcvbnPhp\Matcher;

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

Expand Down
2 changes: 1 addition & 1 deletion src/Matchers/YearMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use ZxcvbnPhp\Matcher;

class YearMatch extends Match
class YearMatch extends BaseMatch
{

public const NUM_YEARS = 119;
Expand Down
15 changes: 8 additions & 7 deletions src/Scorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace ZxcvbnPhp;

use ZxcvbnPhp\Matchers\Bruteforce;
use ZxcvbnPhp\Matchers\Match;
use ZxcvbnPhp\Matchers\BaseMatch;
use ZxcvbnPhp\Matchers\MatchInterface;

/**
* scorer - takes a list of potential matches, ranks and evaluates them,
Expand Down Expand Up @@ -54,7 +55,7 @@ class Scorer
* D^(l-1) approximates Sum(D^i for i in [1..l-1]
*
* @param string $password
* @param Match[] $matches
* @param MatchInterface[] $matches
* @param bool $excludeAdditive
* @return array Returns an array with these keys: [password, guesses, guesses_log10, sequence]
*/
Expand All @@ -75,8 +76,8 @@ public function getMostGuessableMatchSequence($password, $matches, $excludeAddit
// small detail: for deterministic output, sort each sublist by i.
foreach ($matchesByEndIndex as &$matches) {
usort($matches, function ($a, $b) {
/** @var $a Match */
/** @var $b Match */
/** @var $a BaseMatch */
/** @var $b BaseMatch */
return $a->begin - $b->begin;
});
}
Expand All @@ -97,7 +98,7 @@ public function getMostGuessableMatchSequence($password, $matches, $excludeAddit
];

for ($k = 0; $k < $length; $k++) {
/** @var Match $match */
/** @var BaseMatch $match */
foreach ($matchesByEndIndex[$k] as $match) {
if ($match->begin > 0) {
foreach ($this->optimal['m'][$match->begin - 1] as $l => $null) {
Expand Down Expand Up @@ -132,7 +133,7 @@ public function getMostGuessableMatchSequence($password, $matches, $excludeAddit
/**
* helper: considers whether a length-l sequence ending at match m is better (fewer guesses)
* than previously encountered sequences, updating state if so.
* @param Match $match
* @param BaseMatch $match
* @param int $length
*/
protected function update($match, $length)
Expand Down Expand Up @@ -224,7 +225,7 @@ protected function makeBruteforceMatch($begin, $end)
/**
* helper: step backwards through optimal.m starting at the end, constructing the final optimal match sequence.
* @param int $n
* @return Match[] array
* @return MatchInterface[]
*/
protected function unwind($n)
{
Expand Down
10 changes: 5 additions & 5 deletions test/Matchers/L33tTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use ReflectionClass;
use ZxcvbnPhp\Matchers\L33tMatch;
use ZxcvbnPhp\Matchers\Match;
use ZxcvbnPhp\Matchers\BaseMatch;

class L33tTest extends AbstractMatchTest
{
Expand Down Expand Up @@ -295,9 +295,9 @@ public function variationsProvider()
[ 'a8cet', 2, ['8' => 'b'] ],
[ 'abce+', 2, ['+' => 't'] ],
[ '48cet', 4, ['4' => 'a', '8' => 'b'] ],
[ 'a4a4aa', Match::binom(6, 2) + Match::binom(6, 1), ['4' => 'a'] ],
[ '4a4a44', Match::binom(6, 2) + Match::binom(6, 1), ['4' => 'a'] ],
[ 'a44att+', (Match::binom(4, 2) + Match::binom(4, 1)) * Match::binom(3, 1), ['4' => 'a', '+' => 't'] ]
['a4a4aa', BaseMatch::binom(6, 2) + BaseMatch::binom(6, 1), ['4' => 'a'] ],
['4a4a44', BaseMatch::binom(6, 2) + BaseMatch::binom(6, 1), ['4' => 'a'] ],
['a44att+', (BaseMatch::binom(4, 2) + BaseMatch::binom(4, 1)) * BaseMatch::binom(3, 1), ['4' => 'a', '+' => 't'] ]
);
}

Expand Down Expand Up @@ -325,7 +325,7 @@ public function testCapitalisationNotAffectingL33t()
{
$token = 'Aa44aA';
$match = new L33tMatch($token, 0, strlen($token) - 1, $token, ['rank' => 1, 'sub' => ['4' => 'a']]);
$expected = Match::binom(6, 2) + Match::binom(6, 1);
$expected = BaseMatch::binom(6, 2) + BaseMatch::binom(6, 1);

$class = new ReflectionClass('\\ZxcvbnPhp\\Matchers\\L33tMatch');
$method = $class->getMethod('getL33tVariations');
Expand Down
13 changes: 7 additions & 6 deletions test/Matchers/MatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace ZxcvbnPhp\Test\Matchers;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ZxcvbnPhp\Matchers\DateMatch;
use ZxcvbnPhp\Matchers\Match;
use ZxcvbnPhp\Matchers\BaseMatch;
use ZxcvbnPhp\Matchers\MatchInterface;

class MatchTest extends TestCase
Expand All @@ -31,7 +32,7 @@ public function binomialDataProvider()
*/
public function testBinomialCoefficient($n, $k, $expected)
{
$this->assertEquals($expected, Match::binom($n, $k), "binom returns expected result");
$this->assertEquals($expected, BaseMatch::binom($n, $k), "binom returns expected result");
}

public function testBinomialMirrorIdentity()
Expand All @@ -40,8 +41,8 @@ public function testBinomialMirrorIdentity()
$k = 12;

$this->assertEquals(
Match::binom($n, $k),
Match::binom($n, $n - $k),
BaseMatch::binom($n, $k),
BaseMatch::binom($n, $n - $k),
"mirror identity"
);
}
Expand All @@ -52,8 +53,8 @@ public function testBinomialPascalsTriangleIdentity()
$k = 12;

$this->assertEquals(
Match::binom($n, $k),
Match::binom($n - 1, $k - 1) + Match::binom($n - 1, $k),
BaseMatch::binom($n, $k),
BaseMatch::binom($n - 1, $k - 1) + BaseMatch::binom($n - 1, $k),
"pascal's triangle identity"
);
}
Expand Down
4 changes: 2 additions & 2 deletions test/Matchers/MockMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace ZxcvbnPhp\Test\Matchers;

use ZxcvbnPhp\Matchers\Match;
use ZxcvbnPhp\Matchers\BaseMatch;

class MockMatch extends Match
class MockMatch extends BaseMatch
{
protected $guesses;

Expand Down
4 changes: 2 additions & 2 deletions test/Matchers/SpatialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ZxcvbnPhp\Test\Matchers;

use ZxcvbnPhp\Matchers\Match;
use ZxcvbnPhp\Matchers\BaseMatch;
use ZxcvbnPhp\Matchers\SpatialMatch;

/**
Expand Down Expand Up @@ -162,7 +162,7 @@ public function testGuessesShifted()
]);

$this->assertEquals(
$this->getBaseGuessCount($token) * (Match::binom(6, 2) + Match::binom(6, 1)),
$this->getBaseGuessCount($token) * (BaseMatch::binom(6, 2) + BaseMatch::binom(6, 1)),
$match->getGuesses(),
"guesses is added for shifted keys, similar to capitals in dictionary matching"
);
Expand Down
Loading

0 comments on commit 13b505d

Please sign in to comment.