Skip to content

feat: report a check that a string holds a Tailwind class - #2

Merged
sorge-it merged 1 commit into
mainfrom
feat/class-as-string-rule
Oct 6, 2026
Merged

sorge-it merged 1 commit into
mainfrom
feat/class-as-string-rule

Conversation

@sorge-it

@sorge-it sorge-it commented Oct 6, 2026

Copy link
Copy Markdown
Owner

What

A new PHPStan rule, html.classAsString. It reports a check in a test that a string holds a Tailwind class:

expect($html)->toContain('lg:grid-cols-4');                // reported
expect(str_contains($html, 'lg:flex'))->toBeTrue();        // reported
expect($html)->toHaveSelectorClass('[data-x]', 'lg:flex'); // use this instead

Such a check passes when the class is anywhere: on another element, inside a longer class, in a script or in a comment.

How the rule decides

  • Form: a utility with a value in [ ] or after a variant. Plain words, kebab-case and custom properties are not reported. camelCase is reported only with the new parameter htmlAssertions.camelCaseClasses: true.
  • Polarity: only a check that passes when the class is found. A negative check is stricter than a DOM check and is not reported. A search (str_contains, strpos, substr_count, preg_match, Str::) is read only as the value of a check. The check claims found or absent by the kind of the result (bool, position, count, string, collection). A loose comparison such as toEqual(0) says neither.
  • Subject: the value must be surely a string. Arrays, collections, mixed and a class attribute value are not reported.

Other changes

  • StringCalls maps every call to its parameters by position and name. Both rules read it.
  • html.markupAsString also reads the IgnoringCase, IgnoringLineEndings and NotWith assertions, Str::doesntContain and Str::isMatch. It reports a ?-> call one time, not two times.
  • composer.json requires ext-mbstring. The package used mb_trim() already.
  • README section PHPStan, the Boost skill and a new CHANGELOG.md describe the rules.

Test

  • composer test on PHP 8.5: 146 tests, PHPStan max, type coverage 100 %.
  • Each line of the fixture ClassChecks.php names its expected result. Each line was red once.
  • A run over the test suites of 5 product repos and 9 tool apps: 4 reports by default, all correct.

The PHPStan rule html.classAsString reports a positive check of a string,
or of a search result, for a utility with [ ] or after a variant. The
parameter htmlAssertions.camelCaseClasses adds camelCase. StringCalls maps
each call to its parameters and result. html.markupAsString reads more
calls and reports ?-> once. The package requires ext-mbstring.
@sorge-it
sorge-it merged commit 5737470 into main Oct 6, 2026
3 checks passed
@sorge-it
sorge-it deleted the feat/class-as-string-rule branch October 6, 2026 05:10
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