Skip to content

Commit

Permalink
\LastDragon_ru\LaraASP\Documentator\Processor\InstanceFactory helpe…
Browse files Browse the repository at this point in the history
…r to make phpstan happy.
  • Loading branch information
LastDragon-ru committed Dec 13, 2024
1 parent 425c3ad commit 2dab326
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 23 deletions.
14 changes: 7 additions & 7 deletions packages/documentator/src/Commands/Preprocess.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace LastDragon_ru\LaraASP\Documentator\Commands;

use Closure;
use Illuminate\Console\Command;
use LastDragon_ru\LaraASP\Core\Application\ContainerResolver;
use LastDragon_ru\LaraASP\Core\Path\DirectoryPath;
Expand All @@ -12,6 +11,7 @@
use LastDragon_ru\LaraASP\Documentator\Markdown\Mutations\Heading\Renumber;
use LastDragon_ru\LaraASP\Documentator\Package;
use LastDragon_ru\LaraASP\Documentator\Processor\Contracts\Task;
use LastDragon_ru\LaraASP\Documentator\Processor\InstanceFactory;
use LastDragon_ru\LaraASP\Documentator\Processor\Processor;
use LastDragon_ru\LaraASP\Documentator\Processor\Result;
use LastDragon_ru\LaraASP\Documentator\Processor\Tasks\CodeLinks\Task as CodeLinksTask;
Expand Down Expand Up @@ -127,19 +127,19 @@ public function __invoke(Formatter $formatter): void {
private function processor(): Processor {
$processor = new Processor($this->container);

foreach ($this->tasks() as $task => $configurator) {
$processor->task($task, $configurator);
foreach ($this->tasks() as $task) {
$processor->task($task);
}

return $processor;
}

/**
* @return array<class-string<Task>, Closure(Task): void|null>
* @return list<InstanceFactory<covariant Task>|Task|class-string<covariant Task>>
*/
protected function tasks(): array {
return [
PreprocessTask::class => static function (PreprocessTask $task): void {
new InstanceFactory(PreprocessTask::class, static function (PreprocessTask $task): void {
$task->addInstruction(PreprocessIncludeFile::class);
$task->addInstruction(PreprocessIncludeExec::class);
$task->addInstruction(PreprocessIncludeExample::class);
Expand All @@ -149,8 +149,8 @@ protected function tasks(): array {
$task->addInstruction(PreprocessIncludePackageList::class);
$task->addInstruction(PreprocessIncludeDocumentList::class);
$task->addInstruction(PreprocessIncludeGraphqlDirective::class);
},
CodeLinksTask::class => null,
}),
CodeLinksTask::class,
];
}

Expand Down
27 changes: 27 additions & 0 deletions packages/documentator/src/Processor/InstanceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor;

use Closure;

/**
* @see https://github.com/phpstan/phpstan/issues/8770
* @see https://github.com/phpstan/phpstan/issues/9521 (?)
* @see https://github.com/phpstan/phpstan/discussions/11736
*
* @template TInstance of object
*/
readonly class InstanceFactory {
public function __construct(
/**
* @var class-string<TInstance>
*/
public string $class,
/**
* @var Closure(TInstance): void
*/
public Closure $factory,
) {
// empty
}
}
16 changes: 12 additions & 4 deletions packages/documentator/src/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,19 @@ public function tasks(): array {
}

/**
* @param Task|class-string<Task> $task
* @param ($task is object ? null : ?Closure(Task): void) $configurator
* @template T of Task
*
* @param InstanceFactory<covariant T>|T|class-string<T> $task
*/
public function task(Task|string $task, ?Closure $configurator = null): static {
$this->tasks->add($task, $configurator);
public function task(InstanceFactory|Task|string $task): static {
if ($task instanceof InstanceFactory) {
$this->tasks->add(
$task->class, // @phpstan-ignore argument.type (https://github.com/phpstan/phpstan/issues/7609)
$task->factory, // @phpstan-ignore argument.type (https://github.com/phpstan/phpstan/issues/7609)
);
} else {
$this->tasks->add($task);
}

return $this;
}
Expand Down
17 changes: 13 additions & 4 deletions packages/documentator/src/Processor/Tasks/Preprocess/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use LastDragon_ru\LaraASP\Documentator\Processor\Exceptions\ProcessorError;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
use LastDragon_ru\LaraASP\Documentator\Processor\InstanceFactory;
use LastDragon_ru\LaraASP\Documentator\Processor\InstanceList;
use LastDragon_ru\LaraASP\Documentator\Processor\Metadata\Markdown;
use LastDragon_ru\LaraASP\Documentator\Processor\Tasks\Preprocess\Contracts\Instruction;
Expand Down Expand Up @@ -87,12 +88,20 @@ public function getInstructions(): array {
}

/**
* @template I of Instruction<*>
* @template P of Parameters
* @template I of Instruction<P>
*
* @param I|class-string<I> $instruction
* @param InstanceFactory<covariant I>|I|class-string<I> $instruction
*/
public function addInstruction(Instruction|string $instruction): static {
$this->instructions->add($instruction); // @phpstan-ignore argument.type
public function addInstruction(InstanceFactory|Instruction|string $instruction): static {
if ($instruction instanceof InstanceFactory) {
$this->instructions->add(
$instruction->class, // @phpstan-ignore argument.type (https://github.com/phpstan/phpstan/issues/7609)
$instruction->factory, // @phpstan-ignore argument.type (https://github.com/phpstan/phpstan/issues/7609)
);
} else {
$this->instructions->add($instruction);
}

return $this;
}
Expand Down
8 changes: 0 additions & 8 deletions phpstan-baseline-well-known.neon
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,3 @@ parameters:
message: "#^Class `[^`]+` must be marked by `@internal`\\.$#"
paths:
- packages/dev/src/PhpStan/ClassMustBeInternal/RuleTest.php

# Not supported yet (`class-string-map`)
# https://github.com/phpstan/phpstan/discussions/11736
# https://github.com/phpstan/phpstan/issues/9521 (?)
-
message: "#^Method LastDragon_ru\\\\LaraASP\\\\Documentator\\\\Commands\\\\Preprocess::tasks\\(\\) should return array<class-string<LastDragon_ru\\\\LaraASP\\\\Documentator\\\\Processor\\\\Contracts\\\\Task>, \\(Closure\\(LastDragon_ru\\\\LaraASP\\\\Documentator\\\\Processor\\\\Contracts\\\\Task\\): void\\)\\|null> but returns#"
paths:
- packages/documentator/src/Commands/Preprocess.php

0 comments on commit 2dab326

Please sign in to comment.