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
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ jobs:
uses: innmind/github-workflows/.github/workflows/psalm-matrix.yml@main
cs:
uses: innmind/github-workflows/.github/workflows/cs.yml@main
with:
php-version: '8.2'
12 changes: 12 additions & 0 deletions .github/workflows/extensive.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Extensive CI

on:
push:
tags:
- '*'
paths:
- '.github/workflows/extensive.yml'

jobs:
blackbox:
uses: innmind/github-workflows/.github/workflows/extensive.yml@main
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [Unreleased]

### Changed

- Requires PHP `8.4`
- Requires `innmind/immutable:~6.0`
- Requires `innmind/filesystem:~9.0`

## 8.0.0 - 2025-06-01

### Added
Expand Down
4 changes: 4 additions & 0 deletions blackbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

Application::new($argv)
->disableMemoryLimit()
->when(
\getenv('BLACKBOX_SET_SIZE') !== false,
static fn(Application $app) => $app->scenariiPerProof((int) \getenv('BLACKBOX_SET_SIZE')),
)
->when(
\getenv('ENABLE_COVERAGE') !== false,
static fn(Application $app) => $app
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"issues": "http://github.com/Innmind/XML/issues"
},
"require": {
"php": "~8.2",
"innmind/immutable": "~5.15",
"innmind/filesystem": "~8.1"
"php": "~8.4",
"innmind/immutable": "~6.0",
"innmind/filesystem": "~9.0"
},
"autoload": {
"psr-4": {
Expand All @@ -30,7 +30,7 @@
}
},
"require-dev": {
"innmind/static-analysis": "^1.2.1",
"innmind/static-analysis": "~1.3",
"innmind/coding-standard": "~2.0",
"innmind/black-box": "^6.4.1"
}
Expand Down
2 changes: 1 addition & 1 deletion src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private function render(\XMLWriter $writer): Sequence
),
);
/** @psalm-suppress ImpureMethodCall */
$this->type->match(
$_ = $this->type->match(
static fn($type) => $writer->writeRaw($type->toString()."\n"),
static fn() => null,
);
Expand Down
19 changes: 7 additions & 12 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,15 @@ public function __invoke(Content $content): Attempt
return Attempt::error(new \RuntimeException('Empty content'));
}

$xml = new \DOMDocument;
/** @psalm-suppress ImpureMethodCall */
$success = $xml->loadXML(
$content,
\LIBXML_ERR_ERROR | \LIBXML_NOWARNING | \LIBXML_NOERROR,
);

if (!$success) {
return Attempt::error(new \RuntimeException('Failed to load xml content'));
try {
$xml = \Dom\XMLDocument::createFromString(
$content,
\LIBXML_ERR_ERROR | \LIBXML_NOWARNING | \LIBXML_NOERROR,
);
} catch (\Throwable $e) {
return Attempt::error($e);
}

/** @psalm-suppress ImpureMethodCall */
$xml->normalizeDocument();

return ($this->translate)($xml);
}

Expand Down
99 changes: 25 additions & 74 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ private function __construct(
}

/**
* @psalm-suppress UndefinedClass Since the package still supports PHP 8.2
*
* @return Attempt<Document|Node|Element|Custom>
*/
public function __invoke(\DOMNode|\Dom\Node $node): Attempt
public function __invoke(\Dom\Node $node): Attempt
{
return $this
->buildDocument($node)
Expand All @@ -58,62 +56,44 @@ public static function of(?callable $custom = null): self
}

/**
* @psalm-suppress UndefinedClass Since the package still supports PHP 8.2
* @psalm-suppress TypeDoesNotContainType
* @psalm-suppress MixedArgument
* @psalm-suppress MixedMethodCall
* @psalm-suppress MixedPropertyFetch
*
* @return Attempt<Node|Element|Custom>
*/
private function child(\DOMNode|\Dom\Node $node): Attempt
private function child(\Dom\Node $node): Attempt
{
if (
$node->nodeType === \XML_COMMENT_NODE &&
(
$node instanceof \DOMComment ||
$node instanceof \Dom\Comment
)
$node instanceof \Dom\Comment
) {
return Attempt::result(Node::comment($node->data));
}

if (
$node->nodeType === \XML_TEXT_NODE &&
(
$node instanceof \DOMText ||
$node instanceof \Dom\Text
)
$node instanceof \Dom\Text
) {
return Attempt::result(Node::text($node->data));
}

if (
$node->nodeType === \XML_CDATA_SECTION_NODE &&
(
$node instanceof \DOMCharacterData ||
$node instanceof \Dom\CharacterData
)
$node instanceof \Dom\CharacterData
) {
return Attempt::result(Node::characterData($node->data));
}

if (
$node->nodeType === \XML_ENTITY_REF_NODE &&
(
$node instanceof \DOMEntityReference ||
$node instanceof \Dom\EntityReference
)
$node instanceof \Dom\EntityReference
) {
return Attempt::result(Node::entityReference($node->nodeName));
}

if (
$node->nodeType === \XML_PI_NODE &&
(
$node instanceof \DOMProcessingInstruction ||
$node instanceof \Dom\ProcessingInstruction
)
$node instanceof \Dom\ProcessingInstruction
) {
return Attempt::result(Node::processingInstruction(
$node->nodeName,
Expand All @@ -123,30 +103,27 @@ private function child(\DOMNode|\Dom\Node $node): Attempt

if (
$node->nodeType === \XML_ELEMENT_NODE &&
(
$node instanceof \DOMElement ||
$node instanceof \Dom\Element
)
$node instanceof \Dom\Element
) {
// Prefer the local name over the node name to avoid using upper
// case naming when translating html documents (see innmind/html)
$name = (string) ($node->localName ?? $node->nodeName);

/**
* @psalm-suppress ImpureFunctionCall
* @psalm-suppress ImpureMethodCall
*/
return Name::maybe($node->nodeName)
return Name::maybe($name)
->attempt(static fn() => new \RuntimeException(\sprintf(
'Invalid node name "%s"',
$node->nodeName,
$name,
)))
->flatMap(
fn($name) => self::attributes($node)->flatMap(
fn($attributes) => $this
->children(
Sequence::of(...\array_values(\iterator_to_array($node->childNodes)))
->keep(
Instance::of(\DOMNode::class)->or(
Instance::of(\Dom\Node::class),
),
),
->keep(Instance::of(\Dom\Node::class)),
)
->map(static fn($children) => match ($node->childNodes->length) {
0 => Element::selfClosing($name, $attributes),
Expand All @@ -168,22 +145,14 @@ private function child(\DOMNode|\Dom\Node $node): Attempt
}

/**
* @psalm-suppress UndefinedClass Since the package still supports PHP 8.2
* @psalm-suppress MixedArgument
* @psalm-suppress MixedMethodCall
* @psalm-suppress UndefinedPropertyFetch
*
* @return Attempt<Document>
*/
private function buildDocument(\DOMNode|\Dom\Node $node): Attempt
private function buildDocument(\Dom\Node $node): Attempt
{
/** @psalm-suppress MixedArgumentTypeCoercion */
return Maybe::just($node)
->keep(
Instance::of(\DOMDocument::class)->or(
Instance::of(\Dom\Document::class),
),
)
->keep(Instance::of(\Dom\Document::class))
->attempt(static fn() => new \RuntimeException('Not a document'))
->flatMap(
fn($document) => self::buildVersion($document)
Expand All @@ -196,11 +165,7 @@ private function buildDocument(\DOMNode|\Dom\Node $node): Attempt
fn($encoding) => $this
->children(
Sequence::of(...\array_values(\iterator_to_array($document->childNodes)))
->keep(
Instance::of(\DOMNode::class)->or(
Instance::of(\Dom\Node::class),
),
)
->keep(Instance::of(\Dom\Node::class))
->exclude(static fn($child) => $child->nodeType === \XML_DOCUMENT_TYPE_NODE),
)
->map(static fn($children) => Document::of(
Expand All @@ -217,13 +182,12 @@ private function buildDocument(\DOMNode|\Dom\Node $node): Attempt
/**
* @psalm-pure
* @psalm-suppress ImpurePropertyFetch
* @psalm-suppress UndefinedClass Since the package still supports PHP 8.2
*
* @return Maybe<Version>
*/
private static function buildVersion(\DOMDocument|\Dom\Document $document): Maybe
private static function buildVersion(\Dom\Document $document): Maybe
{
[$major, $minor] = \explode('.', $document->xmlVersion ?? '');
[$major, $minor] = \explode('.', (string) ($document->xmlVersion ?? ''));

return Version::maybe(
(int) $major,
Expand All @@ -234,11 +198,10 @@ private static function buildVersion(\DOMDocument|\Dom\Document $document): Mayb
/**
* @psalm-pure
* @psalm-suppress ImpurePropertyFetch
* @psalm-suppress UndefinedClass Since the package still supports PHP 8.2
*
* @return Maybe<Type>
*/
private static function buildDoctype(\DOMDocumentType|\Dom\DocumentType $type): Maybe
private static function buildDoctype(\Dom\DocumentType $type): Maybe
{
/** @psalm-suppress MixedArgument */
return Type::maybe(
Expand All @@ -249,36 +212,26 @@ private static function buildDoctype(\DOMDocumentType|\Dom\DocumentType $type):
}

/**
* @psalm-suppress UndefinedClass Since the package still supports PHP 8.2
* @psalm-suppress TypeDoesNotContainType
* @psalm-suppress MixedArgument
*
* @return Attempt<Sequence<Attribute>>
*/
private static function attributes(\DOMElement|\Dom\Element $element): Attempt
private static function attributes(\Dom\Element $element): Attempt
{
/** @var Sequence<Attribute> */
$attributes = Sequence::of();
$attrs = [];

if (
$element->attributes instanceof \DOMNamedNodeMap ||
$element->attributes instanceof \Dom\NamedNodeMap
) {
if ($element->attributes instanceof \Dom\NamedNodeMap) {
/**
* @psalm-suppress ImpureFunctionCall
* @psalm-suppress ImpureMethodCall
* @psalm-suppress PossiblyInvalidArgument Due to \Dom\NamedNodeMap for PHP 8.2
*/
$attrs = \iterator_to_array($element->attributes);
}

return Sequence::of(...\array_values($attrs))
->keep(
Instance::of(\DOMAttr::class)->or(
Instance::of(\Dom\Attr::class),
),
)
->keep(Instance::of(\Dom\Attr::class))
->sink($attributes)
->attempt(
static fn($attributes, $attribute) => Attribute::maybe(
Expand All @@ -295,9 +248,7 @@ private static function attributes(\DOMElement|\Dom\Element $element): Attempt
}

/**
* @psalm-suppress UndefinedDocblockClass Since the package still supports PHP 8.2
*
* @param Sequence<\DOMNode|\Dom\Node> $children
* @param Sequence<\Dom\Node> $children
*
* @return Attempt<Sequence<Node|Element|Custom>>
*/
Expand Down
8 changes: 4 additions & 4 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public function testPrependChild()
$this->assertSame($document->type(), $document2->type());
$this->assertSame($document->encoding(), $document2->encoding());
$this->assertNotSame($document->children(), $document2->children());
$this->assertCount(3, $document->children());
$this->assertCount(4, $document2->children());
$this->assertSame(3, $document->children()->size());
$this->assertSame(4, $document2->children()->size());
$this->assertSame(
$node,
$document2->children()->get(0)->match(
Expand Down Expand Up @@ -211,8 +211,8 @@ public function testAppendChild()
$this->assertSame($document->type(), $document2->type());
$this->assertSame($document->encoding(), $document2->encoding());
$this->assertNotSame($document->children(), $document2->children());
$this->assertCount(3, $document->children());
$this->assertCount(4, $document2->children());
$this->assertSame(3, $document->children()->size());
$this->assertSame(4, $document2->children()->size());
$this->assertEquals(
$document->children()->get(0),
$document2->children()->get(0),
Expand Down
Loading