konradmichalik / ttt-phpstan
PHPStan rules that point to ttt attributes instead of hand-written test state
Package info
github.com/konradmichalik/ttt-phpstan
Type:phpstan-extension
pkg:composer/konradmichalik/ttt-phpstan
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0
- phpstan/phpstan: ^2.1
Requires (Dev)
- armin/editorconfig-cli: ^2.0
- ergebnis/composer-normalize: ^2.44
- phpunit/phpunit: ^11.5 || ^12.0
Suggests
- konradmichalik/ttt: The attributes the rules point to, a rule stays silent for attributes that are not installed (WithTca needs 0.4 or newer)
Provides
None
Conflicts
None
Replaces
None
README
ttt PHPStan rules
PHPStan rules for ttt. Tests that set putenv() or $GLOBALS by hand depend on a tearDown() that is skipped on hard errors. ttt restores that state declaratively, and these rules point at the attribute that replaces each manual call.
🔥 Installation
Requirements
- PHP 8.2 to 8.6
- PHPStan 2.1 or newer
konradmichalik/ttt, optional: a rule stays silent for attributes that are not installed
Composer
composer require konradmichalik/ttt-phpstan --dev
⚡ Usage
If you have the phpstan/extension-installer
package installed, there is nothing more to do. The rules are included
automatically.
Manual Setup
# phpstan.neon includes: - %rootDir%/../../konradmichalik/ttt-phpstan/rules.neon parameters: paths: - Tests/Unit
🧰 Rules
| Identifier | Reported when | Use instead |
|---|---|---|
ttt.manualGlobals |
$GLOBALS['TYPO3_CONF_VARS'] is assigned below its root |
#[WithTypo3ConfVars] |
ttt.manualGlobals |
$GLOBALS['TCA'] is assigned below its root |
#[WithTca] |
ttt.manualEnvVar |
putenv() is called, to set or to clear a variable |
#[WithEnvVar] |
// Reported: ttt.manualEnvVar putenv('MY_EXT_FEATURE=1'); // Instead #[WithEnvVar('MY_EXT_FEATURE', '1')] public function resolvesConfiguration(): void {}
Only classes that extend PHPUnit's TestCase are checked. FunctionalTestCase is skipped, because ttt rejects some attributes there.
A few cases are deliberately not reported:
- A test class, one of its parent classes or the test method that already carries the matching attribute. ttt restores the whole global afterwards, so more state inside that test is safe.
- Replacing a whole global, as in
$GLOBALS['TYPO3_CONF_VARS'] = [...]. The attributes deep-merge, so they would change what such a test does. - Compound assignments (
??=,.=) andunset(). Only plain=is checked. $GLOBALS['BE_USER']. Tests usually assign a mock with expectations there, which#[WithBackendUser]cannot replace.- Attributes that are not installed.
#[WithTca]needs ttt 0.4 or newer,#[WithEnvVar]and#[WithTypo3ConfVars]exist since 0.1. Without ttt, all rules stay silent.
🛠️ Typical Overrides
A test that needs an invalid value, which a deep merge cannot express, keeps its manual assignment. Ignore the rule inline and keep the reason next to it:
// @phpstan-ignore ttt.manualGlobals (tests an invalid, non-array configuration) $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['my_ext'] = 'not-an-array';
Or ignore one file by identifier and path:
parameters: ignoreErrors: - identifier: ttt.manualEnvVar path: Tests/Unit/Utility/EnvironmentHelperTest.php
🧑💻 Development
The tests use small stubs of the three attributes instead of ttt itself, so the test matrix does not depend on ttt's supported PHP versions.
composer test # PHPUnit composer lint # composer normalize (dry run) and editorconfig check composer fix
⭐ License
This project is licensed under GNU General Public License 3.0 (or later).