Skip to content

PHPStan level max; fix TypeError for buttons with array class - #20

Merged
tomaj merged 2 commits into
masterfrom
ci/php-8.6
Nov 22, 2026
Merged

tomaj merged 2 commits into
masterfrom
ci/php-8.6

Conversation

@tomaj

@tomaj tomaj commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Raises PHPStan from level 8 to max. Reaching max needs one behaviour change, so it is a PR instead of a direct push:

  • render() passed the button's class attribute to strpos(). After $button->getControlPrototype()->addClass(...) the attribute is an array and rendering threw a TypeError. It is now read as a space-separated string of class names, so a button that already has a btn class keeps it and other buttons get btn btn-primary / btn btn-default as before.
  • Output for string class attributes (the previously working case) is unchanged.

🤖 Generated with Claude Code

Html::addClass() stores the class attribute as an array, which made
strpos() in render() throw a TypeError. Read it as a string of class names.
The expected HTML in tests/fixtures was rendered with the code from
master (0b1b49f). Forms that crashed there (button class stored as an
array) are compared with the old output of the same classes set as a
string. Also covers rendering a form twice and part by part, which
crashed before the fix.
@tomaj

tomaj commented Nov 22, 2026

Copy link
Copy Markdown
Owner Author

Review summary

Fix (8218718): correct. classString() reads the class attribute the same way Html::attributes() renders it (name => true and list entries count; false/null entries are skipped). For string classes it still matches btn as a substring, so btn-lg still counts as having btn, as before. The crash was more common than the PR description says: the first render() stores the added btn … class as an array, so rendering the same form a second time or part by part (render($form, 'begin'), 'body', …) also threw a TypeError on master. CHANGELOG updated to say so.

Added (8f3dd10): a PHPUnit suite (tests/, 141 tests). It renders 21 forms (text/password/email/integer, textarea, select/multiselect, checkbox, checkbox list, radio list, upload, hidden, required, errors, groups, buttons with string / addClass() / array / addClass(..., false) classes, primary/default logic, and a complete form) with all three renderers and compares the HTML with fixtures. The fixtures were rendered by master's code (0b1b49f). For the 5 cases that crash on master, the expected HTML is master's output for the same classes set as a string. The suite also checks that rendering twice gives the same HTML, rendering part by part, and novalidate. CI runs phpunit instead of php -l, and PHPStan (level max, phpunit extension) and php-cs-fixer now also check tests/.

Verification

  • Against master's code the suite fails exactly in the crash cases and passes everything else. Mutating the renderer (primary/default swap, form-control, checkbox wrapper, either classString branch, novalidate) makes tests fail.
  • The fixtures are identical with nette/forms 3.2.0 + nette/utils 4.0.4 (prefer-lowest), 3.2.10, 3.3.0 and 3.3.1, and with a different php.ini upload limit. The test forms avoid nette's own version differences: addEmail() adds maxlength in newer versions, and 3.2.0 has a bug in its HTML error item.
  • Locally: PHP 8.3, 8.4, 8.5, 8.6 and 8.3 prefer-lowest all green. PHPStan max, php-cs-fixer, composer validate --strict and composer audit are clean.
  • 0b1b49f: the getContainerPrototype() and $usedPrimary changes give the same output as 2.1.1 (f1fac7e) on both lowest and newest nette/forms. Nothing wrong found.
  • tomaj/nette-api: rendered its API console form (DefaultApiConsoleFormFactory, all 6 authorizations × 3 handlers incl. select/multi/file/json/cookie params) with 2.1.1 and with this PR. The first render is byte-identical; a second render crashed on 2.1.1 and now gives the same HTML. nette-api's own PHPStan passes and its test suite gives the same results with both versions. The only failures are 5 rate-limit storage tests that fail locally either way and don't touch forms.

🤖 Generated with Claude Code

@tomaj
tomaj merged commit bed7abe into master Nov 22, 2026
9 checks passed
@tomaj
tomaj deleted the ci/php-8.6 branch November 22, 2026 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant