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

Refactor #206

Merged
merged 17 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
9c7cfca
feat(documentator)!: `\LastDragon_ru\LaraASP\Documentator\Processor\P…
LastDragon-ru Dec 2, 2024
fa2a512
feat(documentator)!: `\LastDragon_ru\LaraASP\Documentator\Editor\Edit…
LastDragon-ru Dec 7, 2024
8ca1ae8
feat(documentator)!: `\LastDragon_ru\LaraASP\Documentator\Markdown\Co…
LastDragon-ru Dec 9, 2024
0441983
`\LastDragon_ru\LaraASP\Documentator\Markdown\Nodes` namespace rename…
LastDragon-ru Dec 9, 2024
3539137
Better XML renderer for tests.
LastDragon-ru Dec 10, 2024
794a843
Extracted `\LastDragon_ru\LaraASP\Documentator\Markdown\Extensions\Ge…
LastDragon-ru Dec 10, 2024
717cb91
Extracted `\LastDragon_ru\LaraASP\Documentator\Markdown\Extensions\Co…
LastDragon-ru Dec 10, 2024
390c364
Extracted `\LastDragon_ru\LaraASP\Documentator\Markdown\Extensions\Lo…
LastDragon-ru Dec 10, 2024
2be760a
Extracted `\LastDragon_ru\LaraASP\Documentator\Markdown\Extensions\Re…
LastDragon-ru Dec 10, 2024
fef80dd
Tests will use new `\LastDragon_ru\LaraASP\Documentator\Testing\Packa…
LastDragon-ru Dec 10, 2024
d315c8b
`\LastDragon_ru\LaraASP\Documentator\Markdown\Extensions\Reference\Bl…
LastDragon-ru Dec 10, 2024
d16cd8c
`\LastDragon_ru\LaraASP\Documentator\Markdown\Extensions\Reference\Bl…
LastDragon-ru Dec 10, 2024
35f5a2c
`\League\CommonMark\Node\Node` will be used instead of `\LastDragon_r…
LastDragon-ru Dec 10, 2024
f9e5990
refactor(documentator)!: Markdown code reorganization (multiple exten…
LastDragon-ru Dec 10, 2024
dae2af5
feat(documentator): Min version of `league/commonmark` set to `^2.6.0…
LastDragon-ru Dec 10, 2024
ba72abb
test(formatter): `\LastDragon_ru\LaraASP\Formatter\FormatterTest::tes…
LastDragon-ru Dec 10, 2024
4db1265
Composer classmap warnings fix.
LastDragon-ru Dec 10, 2024
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
1 change: 1 addition & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"symbol-whitelist": [
"XMLWriter",
"Composer\\ClassMapGenerator\\ClassMapGenerator",
"Composer\\InstalledVersions",
"Faker\\Factory",
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"illuminate/translation": "^11.0.8",
"illuminate/validation": "^11.0.8",
"larastan/larastan": "^2.8.1",
"league/commonmark": "^2.5.1",
"league/commonmark": "^2.6.0",
"league/config": "^1.1.1",
"mockery/mockery": "^1.6.5",
"nette/neon": "^3.4",
Expand Down Expand Up @@ -92,6 +92,7 @@
"webonyx/graphql-php": "^15.4.0"
},
"require-dev": {
"ext-xmlwriter": "*",
"ext-pdo_sqlite": "*",
"bamarni/composer-bin-plugin": "^1.8",
"composer/class-map-generator": "^1.0",
Expand Down Expand Up @@ -135,6 +136,7 @@
"packages/graphql-printer/docs/",
"packages/graphql/docs/",
"packages/migrator/docs/",
"packages/migrator/src/Migrations/MigratorTest/raw/",
"packages/serializer/docs/",
"packages/spa/docs/",
"packages/testing/docs/"
Expand Down
3 changes: 2 additions & 1 deletion packages/documentator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"illuminate/console": "^11.0.8",
"illuminate/process": "^11.0.8",
"illuminate/support": "^11.0.8",
"league/commonmark": "^2.5.1",
"league/commonmark": "^2.6.0",
"league/config": "^1.1.1",
"nikic/php-parser": "^5.0",
"phpstan/phpdoc-parser": "^1.25",
Expand All @@ -43,6 +43,7 @@
"lastdragon-ru/lara-asp-serializer": "self.version"
},
"require-dev": {
"ext-xmlwriter": "*",
"lastdragon-ru/lara-asp-testing": "self.version",
"mockery/mockery": "^1.6.5",
"orchestra/testbench": "^9.0.0",
Expand Down
12 changes: 9 additions & 3 deletions packages/documentator/src/Commands/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Support\Str;
use LastDragon_ru\LaraASP\Core\Path\DirectoryPath;
use LastDragon_ru\LaraASP\Core\Utils\Cast;
use LastDragon_ru\LaraASP\Documentator\Markdown\Document;
use LastDragon_ru\LaraASP\Documentator\Markdown\Contracts\Markdown;
use LastDragon_ru\LaraASP\Documentator\Markdown\Mutations\Document\Move;
use LastDragon_ru\LaraASP\Documentator\Package;
use LastDragon_ru\LaraASP\Documentator\PackageViewer;
Expand Down Expand Up @@ -36,7 +36,12 @@ class Commands extends Command {
{--defaults : Include application default arguments/options like `--help`, etc.}
SIGNATURE;

public function __invoke(PackageViewer $viewer, Filesystem $filesystem, ArtisanSerializer $serializer): void {
public function __invoke(
PackageViewer $viewer,
Filesystem $filesystem,
Markdown $markdown,
ArtisanSerializer $serializer,
): void {
// Options
$application = Cast::to(Application::class, $this->getApplication());
$namespace = $application->findNamespace(Cast::toString($this->argument('namespace')));
Expand Down Expand Up @@ -68,6 +73,7 @@ static function () use ($filesystem, $target): void {
$this->components->task(
"Command: {$command->getName()}",
static function () use (
$markdown,
$filesystem,
$serializer,
$viewer,
Expand All @@ -94,7 +100,7 @@ static function () use (
'serializer' => $serializer,
'command' => $command,
]);
$content = (string) (new Document($content, $source))->mutate(
$content = (string) $markdown->parse($content, $source)->mutate(
new Move($path),
);

Expand Down
8 changes: 7 additions & 1 deletion packages/documentator/src/Commands/Preprocess.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,13 @@ private function getDocBlock(
): string {
// Load
$this->phpDocumentFactory ??= $this->laravel->make(PhpDocumentFactory::class);
$help = ($this->phpDocumentFactory)($object);
$phpdoc = new PhpDoc((string) $object->getDocComment());
$path = match (true) {
$object instanceof ReflectionProperty => $object->getDeclaringClass()->getFileName(),
default => $object->getFileName(),
};
$path = $path !== false ? new FilePath($path) : null;
$help = ($this->phpDocumentFactory)($phpdoc, $path);

// Move to cwd
$cwd = new DirectoryPath((string) getcwd());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Markdown\Location;
namespace LastDragon_ru\LaraASP\Documentator\Editor;

readonly class Coordinate {
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Markdown;
namespace LastDragon_ru\LaraASP\Documentator\Editor;

use LastDragon_ru\LaraASP\Documentator\Markdown\Location\Coordinate;
use LastDragon_ru\LaraASP\Documentator\Markdown\Location\Location;
use LastDragon_ru\LaraASP\Documentator\Utils\Text;
use Override;
use Stringable;
Expand All @@ -16,53 +14,47 @@
use function array_values;
use function count;
use function implode;
use function is_string;
use function mb_substr;
use function rtrim;
use function usort;

use const PHP_INT_MAX;

/**
* @internal
* @readonly Waiting for no PHP 8.2 (https://github.com/LastDragon-ru/lara-asp/issues/190)
*/
class Editor implements Stringable {
public function __construct(
/**
* @var list<string>
*/
private array $lines,
private int $offset = 0,
/**
* @var list<string>
*/
protected readonly array $lines;

/**
* @param list<string>|string $content
*/
final public function __construct(
array|string $content,
protected readonly int $startLine = 0,
protected readonly string $endOfLine = "\n",
) {
// empty
$this->lines = is_string($content) ? Text::getLines($content) : $content;
}

#[Override]
public function __toString(): string {
return implode("\n", $this->lines);
return implode($this->endOfLine, $this->lines);
}

/**
* @return list<string>
* @param iterable<array-key, Coordinate> $location
*/
public function getLines(): array {
return $this->lines;
}

public function getOffset(): int {
return $this->offset;
}

public function getText(Location|Coordinate $location): ?string {
// Coordinate?
if ($location instanceof Coordinate) {
$location = [$location];
}

public function getText(iterable $location): ?string {
// Select
$selected = null;

foreach ($location as $coordinate) {
$number = $coordinate->line - $this->offset;
$number = $coordinate->line - $this->startLine;

if (isset($this->lines[$number])) {
$selected[] = mb_substr($this->lines[$number], $coordinate->offset, $coordinate->length);
Expand All @@ -77,11 +69,11 @@ public function getText(Location|Coordinate $location): ?string {
}

// Return
return implode("\n", $selected);
return implode($this->endOfLine, $selected);
}

/**
* @param iterable<array-key, array{Location, ?string}> $changes
* @param iterable<array-key, array{iterable<array-key, Coordinate>, ?string}> $changes
*
* @return new<static>
*/
Expand All @@ -100,7 +92,7 @@ public function mutate(iterable $changes): static {
}

// Change
$number = $coordinate->line - $this->offset;
$number = $coordinate->line - $this->startLine;
$line = $lines[$number] ?? '';
$count = count($text);
$prefix = mb_substr($line, 0, $coordinate->offset);
Expand Down Expand Up @@ -131,14 +123,11 @@ public function mutate(iterable $changes): static {
}

// Return
$editor = clone $this;
$editor->lines = array_values($lines);

return $editor;
return new static(array_values($lines), $this->startLine, $this->endOfLine);
}

/**
* @param iterable<array-key, array{Location, ?string}> $changes
* @param iterable<array-key, array{iterable<array-key, Coordinate>, ?string}> $changes
*
* @return list<array{list<Coordinate>, ?string}>
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Markdown;
namespace LastDragon_ru\LaraASP\Documentator\Editor;

use LastDragon_ru\LaraASP\Documentator\Markdown\Location\Append;
use LastDragon_ru\LaraASP\Documentator\Markdown\Location\Coordinate;
use LastDragon_ru\LaraASP\Documentator\Markdown\Location\Location;
use LastDragon_ru\LaraASP\Documentator\Editor\Locations\Append;
use LastDragon_ru\LaraASP\Documentator\Editor\Locations\Location;
use LastDragon_ru\LaraASP\Documentator\Testing\Package\TestCase;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -20,7 +19,7 @@
#[CoversClass(Editor::class)]
final class EditorTest extends TestCase {
public function testMutate(): void {
$lines = [
$lines = [
0 => 'a b c d',
1 => 'e f g h',
2 => 'i j k l',
Expand All @@ -38,8 +37,7 @@ public function testMutate(): void {
14 => '>',
15 => '>',
];
$editor = new Editor($lines, 1);
$changes = [
$changes = [
[new Location(1, 1, 2, 3), "123\n345\n567"],
[new Location(2, 4, 4, 4), '123'],
[new Location(6, 8, 4, 4), "123\n345"],
Expand All @@ -49,6 +47,15 @@ public function testMutate(): void {
[new Location(PHP_INT_MAX, PHP_INT_MAX), "added line a\n"],
[new Location(PHP_INT_MAX, PHP_INT_MAX), "added line b\n"],
];
$editor = new class($lines, 1) extends Editor {
/**
* @return list<string>
*/
public function getLines(): array {
return $this->lines;
}
};

$actual = $editor->mutate($changes);
$expected = [
'a 123',
Expand Down Expand Up @@ -200,7 +207,7 @@ public function testGetText(): void {
<<<'TEXT'
f g
TEXT,
$editor->getText(new Coordinate(2, 2, 3)),
$editor->getText([new Coordinate(2, 2, 3)]),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Markdown\Location;
namespace LastDragon_ru\LaraASP\Documentator\Editor\Locations;

use const PHP_INT_MAX;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Markdown\Location;
namespace LastDragon_ru\LaraASP\Documentator\Editor\Locations;

use IteratorAggregate;
use LastDragon_ru\LaraASP\Documentator\Editor\Coordinate;
use Override;
use Traversable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Markdown\Location;
namespace LastDragon_ru\LaraASP\Documentator\Editor\Locations;

use LastDragon_ru\LaraASP\Documentator\Editor\Coordinate;
use LastDragon_ru\LaraASP\Documentator\Testing\Package\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

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

namespace LastDragon_ru\LaraASP\Documentator\Markdown\Contracts;

use LastDragon_ru\LaraASP\Core\Path\FilePath;
use LastDragon_ru\LaraASP\Documentator\Markdown\Document;

interface Markdown {
public function parse(string $content, ?FilePath $path = null): Document;
}
2 changes: 1 addition & 1 deletion packages/documentator/src/Markdown/Contracts/Mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace LastDragon_ru\LaraASP\Documentator\Markdown\Contracts;

use LastDragon_ru\LaraASP\Documentator\Editor\Locations\Location;
use LastDragon_ru\LaraASP\Documentator\Markdown\Document;
use LastDragon_ru\LaraASP\Documentator\Markdown\Location\Location;

interface Mutation {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/documentator/src/Markdown/Data/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final public function __construct(
/**
* @var T
*/
protected mixed $value,
public mixed $value,
) {
// empty
}
Expand Down
2 changes: 1 addition & 1 deletion packages/documentator/src/Markdown/Data/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace LastDragon_ru\LaraASP\Documentator\Markdown\Data;

use LastDragon_ru\LaraASP\Documentator\Markdown\Location\Location as LocationContract;
use LastDragon_ru\LaraASP\Documentator\Editor\Locations\Location as LocationContract;
use LastDragon_ru\LaraASP\Documentator\Markdown\Utils;
use League\CommonMark\Node\Block\AbstractBlock;
use League\CommonMark\Node\Block\Document;
Expand Down
Loading
Loading