Skip to content

Commit bb6ae9c

Browse files
committed
Prevent adding PHPDoc types instead of native types
PHPDoc types can sometimes be entirely expressed as PHP native types. It is better because it avoids code duplication and enables type runtime check. This will help us slowly migrate away from PHPDoc typing to PHP native typing.
1 parent 2a9f2f5 commit bb6ae9c

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

.github/workflows/main.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ jobs:
6161
FAILURE_ACTION: "${{ matrix.experimental == true }}"
6262
run: vendor/bin/phpunit --verbose || $FAILURE_ACTION
6363

64+
phpdoc-types:
65+
runs-on: ubuntu-latest
66+
steps:
67+
- name: Checkout
68+
uses: actions/checkout@v3
69+
70+
- name: Setup PHP, with composer and extensions
71+
uses: shivammathur/setup-php@v2
72+
with:
73+
php-version: 8.1
74+
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
75+
coverage: none
76+
tools: cs2pr
77+
78+
- name: Check PHPDoc types
79+
run: ./bin/check-phpdoc-types
80+
6481
php-cs-fixer:
6582
runs-on: ubuntu-latest
6683
steps:

bin/check-phpdoc-types

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
/**
5+
* This will check that the "current patch" does not add or modify lines that contain types as PHPDoc when we can express
6+
* them with PHP native types. The "current patch" is either the file about to be committed, the non-committed file, or
7+
* the latest commit.
8+
*
9+
* This will help us slowly migrate away from PHPDoc typing to PHP native typing.
10+
*/
11+
function checkPhpDocTypes(): void
12+
{
13+
$content = `git diff --cached` ?? `git diff` ?? `git show HEAD`;
14+
preg_match_all('~^\+ +\* @(param|var) (mixed|string|int|float|bool|null|array|\?|\|)+ \$\w+$~m', $content, $parameters);
15+
preg_match_all('~^\+ +\* @return (mixed|string|int|float|bool|null|array|void|\?|\|)+$~m', $content, $returns);
16+
17+
$errors = [
18+
...$parameters[0],
19+
...$returns[0],
20+
];
21+
22+
if ($errors) {
23+
echo 'PHP native types must be used instead of PHPDoc types (without comments), for the following lines:' . PHP_EOL . PHP_EOL;
24+
echo join(PHP_EOL, $errors) . PHP_EOL;
25+
exit(1);
26+
}
27+
}
28+
29+
checkPhpDocTypes();

bin/pre-commit

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ if [ "$files" != "" ]; then
2323
echo "$files" | xargs git add
2424
fi
2525

26+
# Check PHPDoc types
27+
./bin/check-phpdoc-types
28+
if [ $? -ne 0 ]; then
29+
pass=false
30+
fi
31+
2632
if $pass; then
2733
exit 0
2834
else

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
],
4343
"scripts": {
4444
"check": [
45+
"./bin/check-phpdoc-types",
4546
"phpcs src/ tests/ --report=checkstyle",
4647
"phpcs --report-width=200 samples/ src/ tests/ --ignore=samples/Header.php --standard=PHPCompatibility --runtime-set testVersion 8.0- -n",
4748
"php-cs-fixer fix --ansi --dry-run --diff",

0 commit comments

Comments
 (0)