Skip to content

Commit

Permalink
Updates for PHP 8.4
Browse files Browse the repository at this point in the history
* Run tests on PHP 8.4 in CI and merge PHPStan and CS Fixer jobs to
  improve performance.
* Update PEST
* Run PHP CS Fixer in parallel
  • Loading branch information
otsch committed Nov 6, 2024
1 parent d778c71 commit 5109fb9
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 57 deletions.
28 changes: 6 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,12 @@ name: CI
on: pull_request

jobs:
cs:
name: PHP CS Fixer
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run PHP CS Fixer
run: composer cs

tests:
name: PestPHP Tests
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['8.1', '8.2', '8.3']
php-versions: ['8.1', '8.2', '8.3', '8.4']

steps:
- name: Checkout code
Expand All @@ -44,8 +25,8 @@ jobs:
- name: Run tests
run: composer test

stan:
name: PHPStan
stanAndCs:
name: Static Analysis (PHPStan) and Code Style (PHP CS Fixer)
runs-on: ubuntu-latest

steps:
Expand All @@ -62,3 +43,6 @@ jobs:

- name: Run PHPStan
run: composer stan

- name: Run PHP CS Fixer
run: composer cs
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor
.php-cs-fixer.cache
.phpunit.result.cache
.phpunit.cache
composer.lock
9 changes: 6 additions & 3 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

use PhpCsFixer\Config;
use PhpCsFixer\Finder;
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;

$finder = Finder::create()
->exclude(['.github', 'bin', 'git-hooks'])
->in(__DIR__);
$config = new Config();

return $config->setFinder($finder)
return (new Config())
->setFinder($finder)
->setParallelConfig(ParallelConfigFactory::detect())
->setRules([
'@PER-CS' => true,
'strict_param' => true,
'array_syntax' => ['syntax' => 'short'],
'no_unused_imports' => true,
])
->setRiskyAllowed(true);
->setRiskyAllowed(true)
->setUsingCache(true);
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.1.2] - 2024-11-06
### Fixed
* Improvements for deprecations in PHP 8.4.

## [0.1.1] - 2024-02-21
### Fixed
- An issue that occurred when the HTML contains something that looks like a charset definition within a `<script>` block.
Expand Down
16 changes: 11 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
"source": "https://github.com/crwlrsoft/html-2-text",
"docs": "https://www.crwlr.software/packages/html-2-text"
},
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/otsch"
}
],
"autoload": {
"psr-4": {
"Crwlr\\Html2Text\\": "src/"
Expand All @@ -31,17 +37,17 @@
"require": {
"php": "^8.1",
"ext-dom": "*",
"masterminds/html5": "^2.8",
"ext-libxml": "*",
"ext-iconv": "*"
"ext-iconv": "*",
"masterminds/html5": "^2.8"
},
"require-dev": {
"pestphp/pest": "^2.19",
"pestphp/pest": "^2.19|^3.0",
"phpstan/phpstan": "^1.10",
"friendsofphp/php-cs-fixer": "^3.30"
"friendsofphp/php-cs-fixer": "^3.57"
},
"scripts": {
"test": "@php vendor/bin/pest",
"test": "@php vendor/bin/pest --display-warnings --bail",
"cs": "@php vendor/bin/php-cs-fixer fix -v --dry-run",
"cs-fix": "@php vendor/bin/php-cs-fixer fix -v",
"stan": "@php vendor/bin/phpstan analyse -c phpstan.neon",
Expand Down
6 changes: 3 additions & 3 deletions src/Html2Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct(readonly public int $indentationSize = self::DEFAULT
*/
public static function convert(
DOMDocument|DOMNodeList|string $html,
int $indentationSize = self::DEFAULT_INDENTATION_SIZE
int $indentationSize = self::DEFAULT_INDENTATION_SIZE,
): string {
return (new self($indentationSize))->convertHtmlToText($html);
}
Expand All @@ -105,12 +105,12 @@ public function convertHtmlToText(DOMDocument|DOMNodeList|string $html): string

public function addConverter(
string|AbstractNodeConverter $nodeNameOrConverterInstance,
string|AbstractNodeConverter $converterClassNameOrInstance = null
null|string|AbstractNodeConverter $converterClassNameOrInstance = null,
): self {
if (is_string($nodeNameOrConverterInstance) && $converterClassNameOrInstance === null) {
throw new InvalidArgumentException(
'When the first argument to this Html2Text::addConverter() is a node name, the second one must ' .
'eiter be a class name or a AbstractNodeConverter instance.'
'eiter be a class name or a AbstractNodeConverter instance.',
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/NodeConverters/Table/TableCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TableCell

public function __construct(
public readonly int $colspan,
public readonly string $text
public readonly string $text,
) {
$this->length = mb_strlen($this->text);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/DomDocumentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ function () {
$document = DomDocumentFactory::make($html);

expect($document)->toBeInstanceOf(DOMDocument::class);
}
},
);
4 changes: 2 additions & 2 deletions tests/Html2TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function convert(DomNodeAndPrecedingText $node): string
});

test('you can add a custom converter for a particular tag', function () {
$nodeConverter = new class () extends AbstractBlockElementWithDefaultMarginConverter {
$nodeConverter = new class extends AbstractBlockElementWithDefaultMarginConverter {
public function nodeName(): string
{
return 'span';
Expand Down Expand Up @@ -167,7 +167,7 @@ function () {
$nodeConverter = $converter->getNodeConverter($node);

expect($nodeConverter)->toBeInstanceOf(FallbackBlockElementConverter::class);
}
},
);

it('removes a node converter for a particular tag', function () {
Expand Down
8 changes: 4 additions & 4 deletions tests/NodeConverters/AbstractBlockElementConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

function helper_divNodeConverterUsingPrecedingTextAsItsOwn(): AbstractBlockElementConverter
{
return new class () extends AbstractBlockElementConverter {
return new class extends AbstractBlockElementConverter {
public function nodeName(): string
{
return 'div';
Expand All @@ -22,7 +22,7 @@ public function convert(DomNodeAndPrecedingText $node): string
}

test('the isBlockElement() method returns true, the others false', function () {
$instance = new class () extends AbstractBlockElementConverter {
$instance = new class extends AbstractBlockElementConverter {
public function nodeName(): string
{
return 'test';
Expand Down Expand Up @@ -52,7 +52,7 @@ function () {
$nodeAndPrecedingText = new DomNodeAndPrecedingText($divEl, 'Lorem ipsum');

expect($nodeConverter->convert($nodeAndPrecedingText))->toBe(PHP_EOL . 'Lorem ipsum' . PHP_EOL);
}
},
);

it(
Expand All @@ -67,5 +67,5 @@ function () {
// return value has a line break at the end, because the added text has. If the addSpacingBeforeAndAfter()
// would have added a line break, there would be two.
expect($nodeConverter->convert($nodeAndPrecedingText))->toBe('Lorem ipsum' . PHP_EOL);
}
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

function helper_pNodeConverterUsingPrecedingTextAsItsOwn(): AbstractBlockElementWithDefaultMarginConverter
{
return new class () extends AbstractBlockElementWithDefaultMarginConverter {
return new class extends AbstractBlockElementWithDefaultMarginConverter {
public function nodeName(): string
{
return 'p';
Expand All @@ -22,7 +22,7 @@ public function convert(DomNodeAndPrecedingText $node): string
}

test('the isBlockElementWithDefaultMargin() method returns true, the others false', function () {
$instance = new class () extends AbstractBlockElementWithDefaultMarginConverter {
$instance = new class extends AbstractBlockElementWithDefaultMarginConverter {
public function nodeName(): string
{
return 'test';
Expand Down Expand Up @@ -52,7 +52,7 @@ function () {
$nodeAndPrecedingText = new DomNodeAndPrecedingText($pEl, 'Pew');

expect($nodeConverter->convert($nodeAndPrecedingText))->toBe(PHP_EOL . PHP_EOL . 'Pew' . PHP_EOL . PHP_EOL);
}
},
);

it('adds one line break before and after, when previous text and text to add, end with a line break', function () {
Expand Down
12 changes: 6 additions & 6 deletions tests/NodeConverters/AbstractInlineElementConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Crwlr\Html2Text\NodeConverters\AbstractInlineElementConverter;

test('the isInlineElement() method returns true, the others false', function () {
$instance = new class () extends AbstractInlineElementConverter {
$instance = new class extends AbstractInlineElementConverter {
public function nodeName(): string
{
return 'test';
Expand All @@ -27,7 +27,7 @@ public function convert(DomNodeAndPrecedingText $node): string
});

it('trims the text on the left side when the preceding text is empty', function () {
$nodeConverter = new class () extends AbstractInlineElementConverter {
$nodeConverter = new class extends AbstractInlineElementConverter {
public function nodeName(): string
{
return 'span';
Expand All @@ -47,7 +47,7 @@ public function convert(DomNodeAndPrecedingText $node): string
});

it('trims the text on the left side when the preceding text ends with a line break', function () {
$nodeConverter = new class () extends AbstractInlineElementConverter {
$nodeConverter = new class extends AbstractInlineElementConverter {
public function nodeName(): string
{
return 'span';
Expand All @@ -67,7 +67,7 @@ public function convert(DomNodeAndPrecedingText $node): string
});

it('trims the text on the left side when the preceding text ends with a space', function () {
$nodeConverter = new class () extends AbstractInlineElementConverter {
$nodeConverter = new class extends AbstractInlineElementConverter {
public function nodeName(): string
{
return 'span';
Expand All @@ -87,7 +87,7 @@ public function convert(DomNodeAndPrecedingText $node): string
});

it('does not trim the text on the left side when the preceding text ends with something else', function () {
$nodeConverter = new class () extends AbstractInlineElementConverter {
$nodeConverter = new class extends AbstractInlineElementConverter {
public function nodeName(): string
{
return 'span';
Expand All @@ -107,7 +107,7 @@ public function convert(DomNodeAndPrecedingText $node): string
});

it('does not trim the text when the isChildOfPreTag property is true', function () {
$nodeConverter = new class () extends AbstractInlineElementConverter {
$nodeConverter = new class extends AbstractInlineElementConverter {
protected bool $isChildOfPreTag = true;

public function nodeName(): string
Expand Down
14 changes: 7 additions & 7 deletions tests/NodeConverters/AbstractNodeConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'when a converter (Html2Text instance) is not set, the getNodeText() method creates a default Html2Text instance' .
'to get an element\'s text',
function () {
$nodeConverter = new class () extends AbstractNodeConverter {
$nodeConverter = new class extends AbstractNodeConverter {
public function nodeName(): string
{
return 'div';
Expand Down Expand Up @@ -59,13 +59,13 @@ protected function addSpacingBeforeAndAfter(string $textToAdd, string $preceding
TEXT);
}
},
);

test(
'when a converter (Html2Text instance) is set, the getNodeText() method uses it to get an element\'s text',
function () {
$nodeConverter = new class () extends AbstractBlockElementConverter {
$nodeConverter = new class extends AbstractBlockElementConverter {
public function nodeName(): string
{
return 'article';
Expand Down Expand Up @@ -115,11 +115,11 @@ protected function addSpacingBeforeAndAfter(string $textToAdd, string $preceding
text after
TEXT);
}
},
);

it('indents text', function () {
$nodeConverter = new class () extends AbstractBlockElementWithDefaultMarginConverter {
$nodeConverter = new class extends AbstractBlockElementWithDefaultMarginConverter {
private int $indentationLevel = 1;

public function nodeName(): string
Expand Down Expand Up @@ -199,7 +199,7 @@ protected function addSpacingBeforeAndAfter(string $textToAdd, string $preceding
test(
'when the isChildOfPreTag property is set to true, the getNodeText() method does not trim and reduce spaces',
function () {
$nodeConverter = new class () extends AbstractBlockElementWithDefaultMarginConverter {
$nodeConverter = new class extends AbstractBlockElementWithDefaultMarginConverter {
protected bool $isChildOfPreTag = true;

public function nodeName(): string
Expand Down Expand Up @@ -235,5 +235,5 @@ public function convert(DomNodeAndPrecedingText $node): string

expect($nodeConverter->convert($node))
->toBe(PHP_EOL . PHP_EOL . PHP_EOL . ' b ' . PHP_EOL . ' ' . PHP_EOL . PHP_EOL);
}
},
);

0 comments on commit 5109fb9

Please sign in to comment.