From cb1cb4aa22d72c55595dceae0b87fb0b4c6f9ddd Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Fri, 28 Oct 2022 14:56:48 +0200 Subject: [PATCH] Support callables on main classes again --- docs/dom.md | 2 +- src/Xml/Dom/Collection/NodeList.php | 43 ++++++++-------- src/Xml/Dom/Document.php | 50 ++++++++++--------- src/Xml/Dom/Xpath.php | 13 +++-- .../ErrorHandling/Issue/IssueCollection.php | 7 ++- src/Xml/Reader/Node/ElementNode.php | 5 +- src/Xml/Reader/Reader.php | 25 +++++----- src/Xml/Util/configure.php | 8 +-- src/Xml/Writer/Writer.php | 17 +++---- src/Xml/Xsd/Schema/SchemaCollection.php | 17 +++---- src/Xml/Xslt/Processor.php | 13 +++-- 11 files changed, 98 insertions(+), 102 deletions(-) diff --git a/docs/dom.md b/docs/dom.md index 8eff9cd8..5828dbaa 100644 --- a/docs/dom.md +++ b/docs/dom.md @@ -25,7 +25,7 @@ $doc = Document::configure( Configurator\validator( Validator\internal_xsd_validator() ), - static fn (DOMDocument $document) => (new MyCustomMergeImportsConfigurator())($document), + new MyCustomMergeImportsConfigurator(), ); $xpath = $doc->xpath( diff --git a/src/Xml/Dom/Collection/NodeList.php b/src/Xml/Dom/Collection/NodeList.php index 9d05ee9d..4d74f689 100644 --- a/src/Xml/Dom/Collection/NodeList.php +++ b/src/Xml/Dom/Collection/NodeList.php @@ -4,7 +4,6 @@ namespace VeeWee\Xml\Dom\Collection; -use Closure; use Countable; use DOMElement; use DOMNode; @@ -104,19 +103,19 @@ public function count(): int /** * @template R - * @param \Closure(T): R $mapper + * @param callable(T): R $mapper * * @return list */ - public function map(Closure $mapper): array + public function map(callable $mapper): array { - return map($this->nodes, $mapper); + return map($this->nodes, $mapper(...)); } /** - * @param \Closure(T): void $mapper + * @param callable(T): void $mapper */ - public function forEach(Closure $mapper): void + public function forEach(callable $mapper): void { foreach ($this->nodes as $node) { $mapper($node); @@ -125,27 +124,27 @@ public function forEach(Closure $mapper): void /** * @template X of DOMNode - * @param \Closure(T): iterable $mapper + * @param callable(T): iterable $mapper * * @return NodeList */ - public function detect(Closure $mapper): self + public function detect(callable $mapper): self { return new self( ...flat_map( $this->nodes, - $mapper + $mapper(...) ) ); } /** - * @param \Closure(T): bool $predicate + * @param callable(T): bool $predicate * @return NodeList */ - public function filter(Closure $predicate): self + public function filter(callable $predicate): self { - return new self(...filter($this->nodes, $predicate)); + return new self(...filter($this->nodes, $predicate(...))); } /** @@ -162,21 +161,21 @@ public function eq(int $index): self /** * @template R - * @param \Closure(R, T): R $reducer + * @param callable(R, T): R $reducer * @param R $initial * @return R */ - public function reduce(Closure $reducer, mixed $initial): mixed + public function reduce(callable $reducer, mixed $initial): mixed { - return reduce($this->nodes, $reducer, $initial); + return reduce($this->nodes, $reducer(...), $initial); } /** - * @param list<\Closure(DOMXPath): DOMXPath> $configurators + * @param list $configurators * @throws RuntimeException * @return NodeList */ - public function query(string $xpath, Closure ... $configurators): self + public function query(string $xpath, callable ... $configurators): self { return $this->detect( /** @@ -190,11 +189,11 @@ public function query(string $xpath, Closure ... $configurators): self /** * @template X - * @param list<\Closure(DOMXPath): DOMXPath> $configurators + * @param list $configurators * @param TypeInterface $type * @return list */ - public function evaluate(string $expression, TypeInterface $type, Closure ... $configurators): array + public function evaluate(string $expression, TypeInterface $type, callable ... $configurators): array { return $this->map( static fn (DOMNode $node): mixed @@ -269,12 +268,12 @@ public function expectAllOfType(string $type): self } /** - * @param \Closure(T, T): int $sorter + * @param callable(T, T): int $sorter * * @return NodeList */ - public function sort(Closure $sorter): self + public function sort(callable $sorter): self { - return new self(...sort($this->nodes, $sorter)); + return new self(...sort($this->nodes, $sorter(...))); } } diff --git a/src/Xml/Dom/Document.php b/src/Xml/Dom/Document.php index d8cca24c..05e15fe1 100644 --- a/src/Xml/Dom/Document.php +++ b/src/Xml/Dom/Document.php @@ -12,6 +12,7 @@ use VeeWee\Xml\Dom\Traverser\Visitor; use VeeWee\Xml\ErrorHandling\Issue\IssueCollection; use VeeWee\Xml\Exception\RuntimeException; +use function Psl\Vec\map; use function VeeWee\Xml\Dom\Configurator\loader; use function VeeWee\Xml\Dom\Loader\xml_file_loader; use function VeeWee\Xml\Dom\Loader\xml_node_loader; @@ -33,11 +34,11 @@ public static function empty(): self } /** - * @param list<\Closure(DOMDocument): DOMDocument> $configurators + * @param list $configurators * * @throws RuntimeException */ - public static function configure(Closure ... $configurators): self + public static function configure(callable ... $configurators): self { $document = configure(...$configurators)(new DOMDocument()); @@ -45,11 +46,11 @@ public static function configure(Closure ... $configurators): self } /** - * @param list<\Closure(DOMDocument): DOMDocument> $configurators + * @param list $configurators * * @throws RuntimeException */ - public static function fromXmlFile(string $file, Closure ...$configurators): self + public static function fromXmlFile(string $file, callable ...$configurators): self { return self::configure( loader(xml_file_loader($file)), @@ -59,11 +60,11 @@ public static function fromXmlFile(string $file, Closure ...$configurators): sel /** * @param non-empty-string $xml - * @param list<\Closure(DOMDocument): DOMDocument> $configurators + * @param list $configurators * * @throws RuntimeException */ - public static function fromXmlString(string $xml, Closure ...$configurators): self + public static function fromXmlString(string $xml, callable ...$configurators): self { return self::configure( loader(xml_string_loader($xml)), @@ -72,11 +73,11 @@ public static function fromXmlString(string $xml, Closure ...$configurators): se } /** - * @param list<\Closure(DOMDocument): DOMDocument> $configurators + * @param list $configurators * * @throws RuntimeException */ - public static function fromXmlNode(DOMNode $node, Closure ...$configurators): self + public static function fromXmlNode(DOMNode $node, callable ...$configurators): self { return self::configure( loader(xml_node_loader($node)), @@ -85,11 +86,11 @@ public static function fromXmlNode(DOMNode $node, Closure ...$configurators): se } /** - * @param list<\Closure(DOMDocument): DOMDocument> $configurators + * @param list $configurators * * @throws RuntimeException */ - public static function fromUnsafeDocument(DOMDocument $document, Closure ...$configurators): self + public static function fromUnsafeDocument(DOMDocument $document, callable ...$configurators): self { return new self( configure(...$configurators)($document) @@ -103,21 +104,21 @@ public function toUnsafeDocument(): DOMDocument /** * @template T - * @param \Closure(DOMDocument): T $locator + * @param callable(DOMDocument): T $locator * * @return T */ - public function locate(Closure $locator) + public function locate(callable $locator) { return $locator($this->document); } /** - * @param \Closure(DOMDocument): mixed $manipulator + * @param callable(DOMDocument): mixed $manipulator * * @return $this */ - public function manipulate(Closure $manipulator): self + public function manipulate(callable $manipulator): self { $manipulator($this->document); @@ -125,38 +126,41 @@ public function manipulate(Closure $manipulator): self } /** - * @param list<\Closure(DOMDocument): (list|DOMNode)> $builders + * @param list|DOMNode)> $builders * * @return list */ - public function build(Closure ... $builders): array + public function build(callable ... $builders): array { - return Builder\nodes(...$builders)($this->document); + return Builder\nodes(...map( + $builders, + fn (callable $builder): Closure => $builder(...) + ))($this->document); } /** - * @param \Closure(DOMDocument): IssueCollection $validator + * @param callable(DOMDocument): IssueCollection $validator */ - public function validate(Closure $validator): IssueCollection + public function validate(callable $validator): IssueCollection { return $validator($this->document); } /** - * @param list<\Closure(DOMXPath): DOMXPath> $configurators + * @param list $configurators */ - public function xpath(Closure ...$configurators): Xpath + public function xpath(callable ...$configurators): Xpath { return Xpath::fromDocument($this, ...$configurators); } /** * @template T - * @param \Closure(DOMDocument): T $mapper + * @param callable(DOMDocument): T $mapper * * @return T */ - public function map(Closure $mapper) + public function map(callable $mapper) { return $mapper($this->document); } diff --git a/src/Xml/Dom/Xpath.php b/src/Xml/Dom/Xpath.php index 5c0f3bff..450b680b 100644 --- a/src/Xml/Dom/Xpath.php +++ b/src/Xml/Dom/Xpath.php @@ -4,7 +4,6 @@ namespace VeeWee\Xml\Dom; -use Closure; use DOMNode; use DOMXPath; use InvalidArgumentException; @@ -25,9 +24,9 @@ private function __construct( } /** - * @param list<\Closure(DOMXPath): DOMXPath> $configurators + * @param list $configurators */ - public static function fromDocument(Document $document, Closure ... $configurators): self + public static function fromDocument(Document $document, callable ... $configurators): self { return new self( configure(...$configurators)(new DOMXPath($document->toUnsafeDocument())) @@ -35,11 +34,11 @@ public static function fromDocument(Document $document, Closure ... $configurato } /** - * @param list<\Closure(DOMXPath): DOMXPath> $configurators + * @param list $configurators * @throws RuntimeException * @throws InvalidArgumentException */ - public static function fromUnsafeNode(DOMNode $node, Closure ... $configurators): self + public static function fromUnsafeNode(DOMNode $node, callable ... $configurators): self { return self::fromDocument( Document::fromUnsafeDocument( @@ -51,12 +50,12 @@ public static function fromUnsafeNode(DOMNode $node, Closure ... $configurators) /** * @template T - * @param \Closure(DOMXpath): T $locator + * @param callable(DOMXpath): T $locator * * @return T * @throws RuntimeException */ - public function locate(Closure $locator) + public function locate(callable $locator) { return $locator($this->xpath); } diff --git a/src/Xml/ErrorHandling/Issue/IssueCollection.php b/src/Xml/ErrorHandling/Issue/IssueCollection.php index 7569db60..a38c3cae 100644 --- a/src/Xml/ErrorHandling/Issue/IssueCollection.php +++ b/src/Xml/ErrorHandling/Issue/IssueCollection.php @@ -4,7 +4,6 @@ namespace VeeWee\Xml\ErrorHandling\Issue; -use Closure; use Countable; use IteratorAggregate; use Psl\Dict; @@ -46,11 +45,11 @@ public function count(): int } /** - * @param (\Closure(Issue): bool) $filter + * @param (callable(Issue): bool) $filter */ - public function filter(Closure $filter): self + public function filter(callable $filter): self { - return new self(...Dict\filter($this->issues, $filter)); + return new self(...Dict\filter($this->issues, $filter(...))); } public function getHighestLevel(): ?Level diff --git a/src/Xml/Reader/Node/ElementNode.php b/src/Xml/Reader/Node/ElementNode.php index f21b0452..a6c98700 100644 --- a/src/Xml/Reader/Node/ElementNode.php +++ b/src/Xml/Reader/Node/ElementNode.php @@ -4,7 +4,6 @@ namespace VeeWee\Xml\Reader\Node; -use Closure; use XMLReader; /** @@ -26,9 +25,9 @@ public function __construct( } /** - * @param \Closure(): list $attributesProvider + * @param callable(): list $attributesProvider */ - public static function fromReader(XMLReader $reader, int $position, Closure $attributesProvider): self + public static function fromReader(XMLReader $reader, int $position, callable $attributesProvider): self { return new self( $position, diff --git a/src/Xml/Reader/Reader.php b/src/Xml/Reader/Reader.php index 87e6d12e..4c769984 100644 --- a/src/Xml/Reader/Reader.php +++ b/src/Xml/Reader/Reader.php @@ -4,7 +4,6 @@ namespace VeeWee\Xml\Reader; -use Closure; use Generator; use VeeWee\Xml\Exception\RuntimeException; use VeeWee\Xml\Reader\Node\AttributeNode; @@ -20,52 +19,52 @@ final class Reader { /** - * @var \Closure(): XMLReader $factory + * @var callable(): XMLReader $factory */ private $factory; /** - * @param \Closure(): XMLReader $factory + * @param callable(): XMLReader $factory */ private function __construct( - Closure $factory + callable $factory ) { $this->factory = $factory; } /** - * @param \Closure(): XMLReader $loader - * @param list<\Closure(XMLReader): XMLReader> $configurators + * @param callable(): XMLReader $loader + * @param list $configurators */ - public static function configure(Closure $loader, Closure ... $configurators): self + public static function configure(callable $loader, callable ... $configurators): self { return new self(static fn () => configure(...$configurators)($loader())); } /** - * @param list<\Closure(XMLReader): XMLReader> $configurators + * @param list $configurators */ - public static function fromXmlFile(string $file, Closure ... $configurators): self + public static function fromXmlFile(string $file, callable ... $configurators): self { return self::configure(xml_file_loader($file), ...$configurators); } /** - * @param list<\Closure(XMLReader): XMLReader> $configurators + * @param list $configurators */ - public static function fromXmlString(string $xml, Closure ... $configurators): self + public static function fromXmlString(string $xml, callable ... $configurators): self { return self::configure(xml_string_loader($xml), ...$configurators); } /** - * @param \Closure(NodeSequence): bool $matcher + * @param callable(NodeSequence): bool $matcher * * @return Generator * * @throws RuntimeException */ - public function provide(Closure $matcher): Generator + public function provide(callable $matcher): Generator { $reader = ($this->factory)(); $pointer = Pointer::create(); diff --git a/src/Xml/Util/configure.php b/src/Xml/Util/configure.php index f7bd1d22..e8e52266 100644 --- a/src/Xml/Util/configure.php +++ b/src/Xml/Util/configure.php @@ -16,23 +16,23 @@ * * @template T * - * @param Closure(T): T ...$stages + * @param callable(T): T ...$stages * * @return Closure(T): T * * @pure */ -function configure(Closure ...$stages): Closure +function configure(callable ...$stages): Closure { return static fn ($input) => Iter\reduce( $stages, /** * @param T $input - * @param (Closure(T): T) $next + * @param (callable(T): T) $next * * @return T */ - static fn (mixed $input, Closure $next): mixed => $next($input), + static fn (mixed $input, callable $next): mixed => $next($input), $input ); } diff --git a/src/Xml/Writer/Writer.php b/src/Xml/Writer/Writer.php index 192adf4a..e2ada3ec 100644 --- a/src/Xml/Writer/Writer.php +++ b/src/Xml/Writer/Writer.php @@ -4,7 +4,6 @@ namespace VeeWee\Xml\Writer; -use Closure; use Generator; use VeeWee\Xml\Exception\RuntimeException; use XMLWriter; @@ -24,26 +23,26 @@ private function __construct(XMLWriter $writer) } /** - * @param list<(\Closure(XMLWriter): XMLWriter)> $configurators + * @param list<(callable(XMLWriter): XMLWriter)> $configurators */ - public static function configure(Closure ... $configurators): self + public static function configure(callable ... $configurators): self { return self::fromUnsafeWriter(new XMLWriter(), ...$configurators); } /** - * @param list<(\Closure(XMLWriter): XMLWriter)> $configurators + * @param list<(callable(XMLWriter): XMLWriter)> $configurators */ - public static function fromUnsafeWriter(XMLWriter $writer, Closure ... $configurators): self + public static function fromUnsafeWriter(XMLWriter $writer, callable ... $configurators): self { return new self(configure(...$configurators)($writer)); } /** - * @param list<(\Closure(XMLWriter): XMLWriter)> $configurators + * @param list<(callable(XMLWriter): XMLWriter)> $configurators * */ - public static function forFile(string $file, Closure ... $configurators): self + public static function forFile(string $file, callable ... $configurators): self { return self::configure( open(xml_file_opener($file)), @@ -52,10 +51,10 @@ public static function forFile(string $file, Closure ... $configurators): self } /** - * @param \Closure(XMLWriter): Generator $writer + * @param callable(XMLWriter): Generator $writer * @throws RuntimeException */ - public function write(Closure $writer): void + public function write(callable $writer): void { $xmlWriter = $this->writer; $cursor = $writer($xmlWriter); diff --git a/src/Xml/Xsd/Schema/SchemaCollection.php b/src/Xml/Xsd/Schema/SchemaCollection.php index 2638bf1c..a80ffa8a 100644 --- a/src/Xml/Xsd/Schema/SchemaCollection.php +++ b/src/Xml/Xsd/Schema/SchemaCollection.php @@ -4,7 +4,6 @@ namespace VeeWee\Xml\Xsd\Schema; -use Closure; use Countable; use IteratorAggregate; use Traversable; @@ -47,32 +46,32 @@ public function add(Schema $schema): self } /** - * @param \Closure(SchemaCollection): SchemaCollection $manipulator + * @param callable(SchemaCollection): SchemaCollection $manipulator */ - public function manipulate(Closure $manipulator): self + public function manipulate(callable $manipulator): self { /** @psalm-suppress ImpureFunctionCall */ return $manipulator($this); } /** - * @param \Closure(Schema): bool $filter + * @param callable(Schema): bool $filter */ - public function filter(Closure $filter): self + public function filter(callable $filter): self { /** @psalm-suppress ImpureFunctionCall */ - return new self(...filter($this->schemas, $filter)); + return new self(...filter($this->schemas, $filter(...))); } /** * @template T - * @param \Closure(Schema): T $mapper + * @param callable(Schema): T $mapper * * @return list */ - public function map(Closure $mapper) + public function map(callable $mapper) { /** @psalm-suppress ImpureFunctionCall */ - return map($this->schemas, $mapper); + return map($this->schemas, $mapper(...)); } } diff --git a/src/Xml/Xslt/Processor.php b/src/Xml/Xslt/Processor.php index a3f69ac5..2e2df2a5 100644 --- a/src/Xml/Xslt/Processor.php +++ b/src/Xml/Xslt/Processor.php @@ -4,7 +4,6 @@ namespace VeeWee\Xml\Xslt; -use Closure; use VeeWee\Xml\Dom\Document; use XSLTProcessor; use function VeeWee\Xml\Dom\Mapper\from_template_document; @@ -22,9 +21,9 @@ private function __construct(XSLTProcessor $processor) } /** - * @param list<\Closure(XSLTProcessor): XSLTProcessor> $configurators + * @param list $configurators */ - public static function configure(Closure ... $configurators): self + public static function configure(callable ... $configurators): self { return new self( configure(...$configurators)(new XSLTProcessor()) @@ -32,9 +31,9 @@ public static function configure(Closure ... $configurators): self } /** - * @param list<\Closure(XSLTProcessor): XSLTProcessor> $configurators + * @param list $configurators */ - public static function fromTemplateDocument(Document $template, Closure ... $configurators): self + public static function fromTemplateDocument(Document $template, callable ... $configurators): self { return self::configure( loader(from_template_document($template)), @@ -44,10 +43,10 @@ public static function fromTemplateDocument(Document $template, Closure ... $con /** * @template T - * @param \Closure(XSLTProcessor): T $transformer + * @param callable(XSLTProcessor): T $transformer * @return T */ - public function transform(Closure $transformer): mixed + public function transform(callable $transformer): mixed { return $transformer($this->processor); }