zdenekgebauer / password-strength-meter
check strength of passwords
Package info
gitlab.com/zdenekgebauer/password-strength-meter
pkg:composer/zdenekgebauer/password-strength-meter
Requires
- php: >=8.4
- ext-mbstring: *
Requires (Dev)
- codeception/codeception: ^5.3
- codeception/module-asserts: ^3.3
- infection/codeception-adapter: ^0.4
- infection/infection: ^0.35
- phpstan/phpstan: ^2.3
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.1
- rector/rector: ^2.9
- shipmonk/composer-dependency-analyser: ^1.8
- symplify/easy-coding-standard: ^13.3
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-01 10:44:28 UTC
README
PHP 8.4+ library for validating password strength by configurable rules.
Requirements
PHP 8.4 or newer. No runtime dependencies beyond ext-mbstring.
Installation
composer require zdenekgebauer/password-strength-meter
Upgrading from 1.x: version 2.0.0 contains breaking changes, see the BC break entries in CHANGELOG.md.
Usage
use ZdenekGebauer\PasswordStrengthMeter\Constraints;
use ZdenekGebauer\PasswordStrengthMeter\Validator;
Check password by rules
$constraints = new Constraints();
$constraints->minLength = 10; // default: Constraints::MIN_LENGTH (4)
$constraints->requiredNumber = true;
$constraints->requiredUppercase = true;
$constraints->requiredLowercase = true;
$constraints->requiredSpecialChar = true;
$result = (new Validator($constraints))->validate('aikfF56$');
if ($result->isStrong()) {
echo 'password is OK';
} else {
echo 'password is WEAK';
foreach ($result->getViolations() as $violation) {
echo $violation->value;
}
}
Violations
Result::getViolations() returns list<Violation>. Violation is a backed enum;
its value is a stable identifier safe to use as a translation key — it will not
change without a major version bump. Possible values:
Violation case | Value | Condition |
|---|---|---|
TOO_SHORT | too-short | length < minLength |
FORBIDDEN_TEXT | forbidden-text | contains a forbidden text (case-insensitive) |
REPEATED_CHARS | repeated-chars | 3+ identical characters in a row |
NO_NUMBER | no-number | no digit, requiredNumber = true |
NO_UPPER | no-upper-char | no uppercase, requiredUppercase = true |
NO_LOWER | no-lower-char | no lowercase, requiredLowercase = true |
NO_SPECIAL | no-special-char | no special char, requiredSpecialChar = true |
All rules work with characters, not bytes, so multibyte passwords are evaluated correctly:
Ť counts as an uppercase letter and ššš as three repeated characters.
A single rule can be checked with Result::hasViolation():
if ($result->hasViolation(Violation::TOO_SHORT)) {
echo 'password is too short';
}
Forbidden texts
Default forbidden list is available as Constraints::DEFAULT_FORBIDDEN_TEXTS.
Additional texts can be added (matching is case-insensitive):
$constraints = new Constraints();
$constraints->addForbiddenTexts(['heslo', 'iloveyou']);
The current list is readable through $constraints->forbiddenTexts, but it is
read-only from the outside — addForbiddenTexts() is the only way to extend it.
Because matching ignores case, texts differing only in case are treated as
duplicates and added just once.
Special characters
Default set is available as Constraints::DEFAULT_SPECIAL_CHARS.
Can be replaced with a custom set:
$constraints = new Constraints();
$constraints->specialChars = '!@#$%';
The value is a plain string; the validator splits it into characters itself, multibyte
characters such as € included.
Exceptions
\InvalidArgumentException is thrown when:
minLengthis set belowConstraints::MIN_LENGTH(4),specialCharsis assigned an empty string.
Validator::validate() never throws. The password parameter is marked with
#[\SensitiveParameter], so it is not exposed in stack traces.
Development
Local environment runs on Lando (Docker, PHP 8.5 + xdebug).
lando install # install dependencies
lando tests # run all tests
lando coverage # code coverage
lando dependency-analyser # composer.json dependencies against their usage in code
lando ecs # coding standards (lando ecs-fix to apply)
lando infection # mutation testing, requires 100 % MSI
lando phpstan # static analysis, level max
lando rector # code modernization dry-run (lando rector-fix to apply)