Skip to content

Commit

Permalink
Add missing assertions to the mixin classes
Browse files Browse the repository at this point in the history
After significant changes to the script that updates the interface we
use as a mixing PHP DocBlock [1], we did not include the "keyPresent" and
"keyNotPresent" assertions.

This commit includes the missing assertions but also refactors the
script to have less duplication.

[1]: 897693d

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
  • Loading branch information
henriquemoody committed Mar 21, 2023
1 parent b3c5d2c commit 44c0d97
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
73 changes: 38 additions & 35 deletions bin/update-assertions
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ exclude-function()
grep --invert-match "public function ${1}("
}

add-prefixes()
{
local prefix="${1}"
local rules="${2}"
local extra_parameter="${3:-}"

if [ ! -z "${extra_parameter}" ]; then
extra_parameter="${extra_parameter}, "
fi

sed --regexp-extended "s/public function ([^(]+)\(/public function ${prefix}\u\1(${extra_parameter}/g" "${rules}" |
sed "s,${prefix}Equals,${prefix},g"
}

add-function()
{
local name="${1}"
local extra_parameter="${2:-}"

if [ ! -z "${extra_parameter}" ]; then
extra_parameter="${extra_parameter}, "
fi

echo "public function ${name}(${extra_parameter}Throwable|string|null \$description = null): self;"
}

echo "- Parsing all rules"
cp "${ORIGINAL_VALIDATOR}" "${TRANSFORMED_VALIDATOR}"
vendor/bin/phpcbf -q "${TRANSFORMED_VALIDATOR}" || true
Expand Down Expand Up @@ -55,41 +81,18 @@ vendor/bin/phpcbf -q "${TRANSFORMED_VALIDATOR}" || true
# original rules
cat "${CANONICAL_RULES}"


# "all" rules
sed --regexp-extended "s,public function ([^(]+)\(,public function all\u\1(,g" "${CANONICAL_RULES}" |
sed "s,allEquals,all,g"

# "allNot" rules
sed --regexp-extended "s,public function ([^(]+)\(,public function allNot\u\1(,g" "${CANONICAL_RULES}" |
sed "s,allNotEquals,allNot,g"

# "key" rules
sed --regexp-extended "s/public function ([^(]+)\(/public function key\u\1(mixed \$key, /g; s/, \)/)/g" "${CANONICAL_RULES}" |
sed "s,keyEquals,key,g"

# "keyNot" rules
sed --regexp-extended "s/public function ([^(]+)\(/public function keyNot\u\1(mixed \$key, /g; s/, \)/)/g" "${CANONICAL_RULES}" |
sed "s,keyNotEquals,keyNot,g"

# "not" rules
sed --regexp-extended "s,public function ([^(]+)\(,public function not\u\1(,g" "${CANONICAL_RULES}" |
sed "s,notEquals,not,g"

sed --regexp-extended "s,public function ([^(]+)\(,public function nullOr\u\1(,g" "${CANONICAL_RULES}" |
sed "s,nullOrEquals,nullOr,g"

sed --regexp-extended "s,public function ([^(]+)\(,public function nullOrNot\u\1(,g" "${CANONICAL_RULES}" |
sed "s,nullOrNotEquals,nullOrNot,g"

sed --regexp-extended "s,public function ([^(]+)\(,public function length\u\1(,g" "${SPECIAL_RULES}" |
sed "s,lengthEquals,length,g"

sed --regexp-extended "s,public function ([^(]+)\(,public function max\u\1(,g" "${SPECIAL_RULES}" |
sed "s,maxEquals,max,g"

sed --regexp-extended "s,public function ([^(]+)\(,public function min\u\1(,g" "${SPECIAL_RULES}" |
sed "s,minEquals,min,g"
add-prefixes "all" "${CANONICAL_RULES}"
add-prefixes "allNot" "${CANONICAL_RULES}"
add-prefixes "key" "${CANONICAL_RULES}" "mixed \$key"
add-function "keyPresent" "mixed \$key"
add-prefixes "keyNot" "${CANONICAL_RULES}" "mixed \$key"
add-function "keyNotPresent" "mixed \$key"
add-prefixes "not" "${CANONICAL_RULES}"
add-prefixes "nullOr" "${CANONICAL_RULES}"
add-prefixes "nullOrNot" "${CANONICAL_RULES}"
add-prefixes "length" "${SPECIAL_RULES}"
add-prefixes "max" "${SPECIAL_RULES}"
add-prefixes "min" "${SPECIAL_RULES}"
} > "${ALL_RULES}"

rm "${TRANSFORMED_VALIDATOR}"
Expand Down
4 changes: 4 additions & 0 deletions src/AssertMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,8 @@ public static function keyXdigit(mixed $input, mixed $key, string $additionalCha

public static function keyYes(mixed $input, mixed $key, bool $useLocale = false, Throwable|string|null $description = null): void;

public static function keyPresent(mixed $input, mixed $key, Throwable|string|null $description = null): void;

public static function keyNotAlnum(mixed $input, mixed $key, string $additionalChars = '', Throwable|string|null $description = null): void;

public static function keyNotAlpha(mixed $input, mixed $key, string $additionalChars = '', Throwable|string|null $description = null): void;
Expand Down Expand Up @@ -1364,6 +1366,8 @@ public static function keyNotXdigit(mixed $input, mixed $key, string $additional

public static function keyNotYes(mixed $input, mixed $key, bool $useLocale = false, Throwable|string|null $description = null): void;

public static function keyNotPresent(mixed $input, mixed $key, Throwable|string|null $description = null): void;

public static function notAlnum(mixed $input, string $additionalChars = '', Throwable|string|null $description = null): void;

public static function notAlpha(mixed $input, string $additionalChars = '', Throwable|string|null $description = null): void;
Expand Down
4 changes: 4 additions & 0 deletions src/ChainAssertMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,8 @@ public function keyXdigit(mixed $key, string $additionalChars = '', Throwable|st

public function keyYes(mixed $key, bool $useLocale = false, Throwable|string|null $description = null): self;

public function keyPresent(mixed $key, Throwable|string|null $description = null): self;

public function keyNotAlnum(mixed $key, string $additionalChars = '', Throwable|string|null $description = null): self;

public function keyNotAlpha(mixed $key, string $additionalChars = '', Throwable|string|null $description = null): self;
Expand Down Expand Up @@ -1364,6 +1366,8 @@ public function keyNotXdigit(mixed $key, string $additionalChars = '', Throwable

public function keyNotYes(mixed $key, bool $useLocale = false, Throwable|string|null $description = null): self;

public function keyNotPresent(mixed $key, Throwable|string|null $description = null): self;

public function notAlnum(string $additionalChars = '', Throwable|string|null $description = null): self;

public function notAlpha(string $additionalChars = '', Throwable|string|null $description = null): self;
Expand Down

0 comments on commit 44c0d97

Please sign in to comment.