Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CI pipelines #239

Merged
merged 2 commits into from
Jul 12, 2023
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
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.2', '7.3', '7.4']
php: ['7.2', '7.3', '7.4', '8.0', '8.1']
coverage: [true]
composer-flags: ['']
include:
- php: '8.0'
- php: '8.2'
coverage: false
composer-flags: '--ignore-platform-req=php'
- php: '7.2'
Expand All @@ -53,7 +53,7 @@ jobs:

- name: "Use PHPUnit 9.3+ on PHP 8"
run: composer require --no-update --dev phpunit/phpunit:^9.3
if: "matrix.php == '8.0'"
if: "matrix.php >= '8.0'"

- run: composer update --no-progress ${{ matrix.composer-flags }}

Expand Down
35 changes: 35 additions & 0 deletions src/Coerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace League\HTMLToMarkdown;

/**
* @internal
*/
final class Coerce
{
private function __construct()
{
}

/**
* @param mixed $val
*/
public static function toString($val): string
{
switch (true) {
case \is_string($val):
return $val;
case \is_bool($val):
case \is_float($val):
case \is_int($val):
case $val === null:
return \strval($val);
case \is_object($val) && \method_exists($val, '__toString'):
return $val->__toString();
default:
throw new \InvalidArgumentException('Cannot coerce this value to string');
}
}
}
5 changes: 3 additions & 2 deletions src/Converter/ListItemConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace League\HTMLToMarkdown\Converter;

use League\HTMLToMarkdown\Coerce;
use League\HTMLToMarkdown\Configuration;
use League\HTMLToMarkdown\ConfigurationAwareInterface;
use League\HTMLToMarkdown\ElementInterface;
Expand Down Expand Up @@ -38,8 +39,8 @@ public function convert(ElementInterface $element): string
}

if ($listType === 'ul') {
$listItemStyle = \strval($this->config->getOption('list_item_style', '-'));
$listItemStyleAlternate = \strval($this->config->getOption('list_item_style_alternate', ''));
$listItemStyle = Coerce::toString($this->config->getOption('list_item_style', '-'));
$listItemStyleAlternate = Coerce::toString($this->config->getOption('list_item_style_alternate', ''));
if (! isset($this->listItemStyle)) {
$this->listItemStyle = $listItemStyleAlternate ?: $listItemStyle;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Converter/TableConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace League\HTMLToMarkdown\Converter;

use League\HTMLToMarkdown\Coerce;
use League\HTMLToMarkdown\Configuration;
use League\HTMLToMarkdown\ConfigurationAwareInterface;
use League\HTMLToMarkdown\ElementInterface;
Expand Down Expand Up @@ -89,7 +90,7 @@ public function convert(ElementInterface $element): string
}

$value = \str_replace("\n", ' ', $value);
$value = \str_replace('|', \strval($this->config->getOption('table_pipe_escape') ?? '\|'), $value);
$value = \str_replace('|', Coerce::toString($this->config->getOption('table_pipe_escape') ?? '\|'), $value);

return '| ' . \trim($value) . ' ';
case 'thead':
Expand Down
6 changes: 1 addition & 5 deletions src/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ public function isDescendantOf($tagNames): bool
$tagNames = [$tagNames];
}

for ($p = $this->node->parentNode; $p !== false; $p = $p->parentNode) {
if ($p === null) {
return false;
}

for ($p = $this->node->parentNode; $p !== null; $p = $p->parentNode) {
if (\in_array($p->nodeName, $tagNames, true)) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/HtmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ protected function convertToMarkdown(ElementInterface $element): string
$tag = $element->getTagName();

// Strip nodes named in remove_nodes
$tagsToRemove = \explode(' ', \strval($this->getConfig()->getOption('remove_nodes') ?? ''));
$tagsToRemove = \explode(' ', Coerce::toString($this->getConfig()->getOption('remove_nodes') ?? ''));
if (\in_array($tag, $tagsToRemove, true)) {
return '';
}
Expand Down
56 changes: 56 additions & 0 deletions tests/CoerceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace League\HTMLToMarkdown\Test;

use League\HTMLToMarkdown\Coerce;
use PHPUnit\Framework\TestCase;

final class CoerceTest extends TestCase
{
/**
* @dataProvider provideStringTestCases
*
* @param mixed $val
*/
public function testToString($val, string $expected): void
{
$this->assertSame($expected, Coerce::toString($val));
}

public function provideStringTestCases(): \Generator
{
yield ['foo', 'foo'];
yield [1, '1'];
yield [1.1, '1.1'];
yield [true, '1'];
yield [false, ''];
yield [null, ''];
yield [$this, $this->__toString()];
}

/**
* @dataProvider provideInvalidStringTestCases
*
* @param mixed $val
*/
public function testToStringThrowsOnUncoercableValue($val): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot coerce this value to string');

Coerce::toString($val);
}

public function provideInvalidStringTestCases(): \Generator
{
yield [new \stdClass()];
yield [STDOUT];
}

public function __toString(): string
{
return 'some object';
}
}
Loading