Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ permissions:
contents: read

jobs:
lint:
name: Lint PHP ${{ matrix.php }} ${{ matrix.dependency-version }}
tests:
name: PHP ${{ matrix.php }} ${{ matrix.dependency-version }}
runs-on: ubuntu-latest
# PHP 8.7 is a nightly build: it runs to show problems early but does not fail the workflow
continue-on-error: ${{ matrix.php == '8.7' }}
Expand Down Expand Up @@ -50,9 +50,8 @@ jobs:
# dependencies cap their PHP version, so the nightly ignores the upper bound
run: composer update ${{ matrix.dependency-version }} ${{ matrix.php == '8.7' && '--ignore-platform-req=php+' || '' }} --prefer-dist --no-progress --no-interaction

- name: Lint
# the package has no test suite; check that every file parses on this PHP version
run: find src -name '*.php' -print0 | xargs -0 -n1 php -l
- name: Run tests
run: vendor/bin/phpunit --no-coverage

code-style:
name: Code Style (PER-CS + PHP 8.3)
Expand All @@ -75,7 +74,7 @@ jobs:
run: vendor/bin/php-cs-fixer fix --dry-run --diff --verbose

static-analysis:
name: Static Analysis (PHPStan level 8)
name: Static Analysis (PHPStan level max)
runs-on: ubuntu-latest

steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ vendor
.DS_Store
composer.lock
build/
.phpunit.cache/
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in([__DIR__ . '/src', __DIR__ . '/tests'])
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file.
- **BC break:** minimum PHP version raised from 7.1 to 8.3 (`"php": "^8.3"`). The package already needed PHP 8.1+ through nette/forms 3.2.
- `Checkbox` controls use `getContainerPrototype()` instead of the deprecated `getSeparatorPrototype()` alias (same element, no change in output).

### Fixed
- Rendering no longer fails with a `TypeError` when a button's class is stored as an array: set with `addClass()` or as an array, and also when the same form is rendered a second time or part by part (`render($form, 'begin')`, `'body'`, ...), because the first render adds the `btn` classes that way. The existing classes are checked for `btn`; output for forms that rendered before is unchanged.

### Added
- GitHub Actions CI: lint on PHP 8.3–8.6 (plus prefer-lowest, nightly 8.7 allowed to fail), php-cs-fixer (PER-CS), PHPStan with strict rules, composer audit.
- PHPUnit test suite that renders forms with all three renderers and compares the HTML with the output from before the fix.
- GitHub Actions CI: tests on PHP 8.3–8.6 (plus prefer-lowest, nightly 8.7 allowed to fail), php-cs-fixer (PER-CS), PHPStan level max with strict rules, composer audit.
- Dependabot for Composer and GitHub Actions.
19 changes: 15 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,34 @@
"nette/phpstan-rules": "^1.0.8",
"phpstan/phpstan": "^2.3",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0"
"phpstan/phpstan-phpunit": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^12.5"
},
"autoload": {
"classmap": [
"src/"
]
},
"autoload-dev": {
"psr-4": {
"Tomaj\\Form\\Renderer\\Tests\\": "tests/"
}
},
"scripts": {
"check": [
"@phpcs",
"@phpstan",
"@lint"
"@test"
],
"cs-fix": "php-cs-fixer fix",
"lint": "find src -name '*.php' -print0 | xargs -0 -n1 php -l",
"phpcs": "php-cs-fixer fix --dry-run --diff --verbose",
"phpstan": "phpstan analyse --memory-limit=1G"
"phpstan": "phpstan analyse --memory-limit=1G",
"phpunit": [
"@putenv XDEBUG_MODE=off",
"phpunit --no-coverage"
],
"test": "@phpunit"
},
"config": {
"sort-packages": true
Expand Down
5 changes: 4 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/nette/phpstan-rules/extension.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon

parameters:
level: 8
level: max
paths:
- src
- tests
reportUnmatchedIgnoredErrors: true
13 changes: 13 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache" executionOrder="defects" beStrictAboutOutputDuringTests="true" displayDetailsOnPhpunitDeprecations="true" displayDetailsOnPhpunitNotices="true" displayDetailsOnTestsThatTriggerDeprecations="true" displayDetailsOnTestsThatTriggerNotices="true" displayDetailsOnTestsThatTriggerWarnings="true" failOnDeprecation="true" failOnEmptyTestSuite="true" failOnNotice="true" failOnPhpunitDeprecation="true" failOnPhpunitNotice="true" failOnPhpunitWarning="true" failOnRisky="true" failOnWarning="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<source ignoreIndirectDeprecations="true">
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
22 changes: 21 additions & 1 deletion src/BootstrapInlineRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function render(Form $form, ?string $mode = null): string
$usedPrimary = false;
foreach ($form->getControls() as $control) {
if ($control instanceof Button) {
if (strpos($control->getControlPrototype()->getClass() ?? '', 'btn') === false) {
if (!str_contains(self::classString($control->getControlPrototype()->getClass()), 'btn')) {
$control->getControlPrototype()->addClass($usedPrimary ? 'btn btn-default' : 'btn btn-primary');
$usedPrimary = true;
}
Expand All @@ -99,4 +99,24 @@ public function render(Form $form, ?string $mode = null): string

return parent::render($form, $mode);
}

/**
* The class attribute is a string, or an array of class => enabled after Html::addClass().
*/
private static function classString(mixed $class): string
{
if (is_array($class)) {
$classes = [];
foreach ($class as $name => $value) {
if ($value === true) {
$classes[] = (string) $name;
} elseif (is_string($value) && $value !== '') {
$classes[] = $value;
}
}
return implode(' ', $classes);
}

return is_string($class) ? $class : '';
}
}
22 changes: 21 additions & 1 deletion src/BootstrapRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function render(Form $form, ?string $mode = null): string
$usedPrimary = false;
foreach ($form->getControls() as $control) {
if ($control instanceof Button) {
if (strpos($control->getControlPrototype()->getClass() ?? '', 'btn') === false) {
if (!str_contains(self::classString($control->getControlPrototype()->getClass()), 'btn')) {
$control->getControlPrototype()->addClass($usedPrimary ? 'btn btn-default' : 'btn btn-primary');
$usedPrimary = true;
}
Expand All @@ -99,4 +99,24 @@ public function render(Form $form, ?string $mode = null): string

return parent::render($form, $mode);
}

/**
* The class attribute is a string, or an array of class => enabled after Html::addClass().
*/
private static function classString(mixed $class): string
{
if (is_array($class)) {
$classes = [];
foreach ($class as $name => $value) {
if ($value === true) {
$classes[] = (string) $name;
} elseif (is_string($value) && $value !== '') {
$classes[] = $value;
}
}
return implode(' ', $classes);
}

return is_string($class) ? $class : '';
}
}
22 changes: 21 additions & 1 deletion src/BootstrapVerticalRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function render(Form $form, ?string $mode = null): string
$usedPrimary = false;
foreach ($form->getControls() as $control) {
if ($control instanceof Button) {
if (strpos($control->getControlPrototype()->getClass() ?? '', 'btn') === false) {
if (!str_contains(self::classString($control->getControlPrototype()->getClass()), 'btn')) {
$control->getControlPrototype()->addClass($usedPrimary ? 'btn btn-default' : 'btn btn-primary');
$usedPrimary = true;
}
Expand All @@ -97,4 +97,24 @@ public function render(Form $form, ?string $mode = null): string

return parent::render($form, $mode);
}

/**
* The class attribute is a string, or an array of class => enabled after Html::addClass().
*/
private static function classString(mixed $class): string
{
if (is_array($class)) {
$classes = [];
foreach ($class as $name => $value) {
if ($value === true) {
$classes[] = (string) $name;
} elseif (is_string($value) && $value !== '') {
$classes[] = $value;
}
}
return implode(' ', $classes);
}

return is_string($class) ? $class : '';
}
}
Loading