Skip to content

Commit

Permalink
Fix PHP DocComments
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Mar 27, 2024
1 parent b48d5cf commit b9739ac
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 48 deletions.
16 changes: 2 additions & 14 deletions src/Languages/DocComment/DocCommentLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@
namespace Tempest\Highlight\Languages\DocComment;

use Tempest\Highlight\Languages\Base\BaseLanguage;
use Tempest\Highlight\Languages\DocComment\Injections\GenericTypeInjection;
use Tempest\Highlight\Languages\DocComment\Patterns\DocCommentGenericTypePattern;
use Tempest\Highlight\Languages\DocComment\Patterns\DocCommentParamTypePattern;
use Tempest\Highlight\Languages\DocComment\Patterns\DocCommentReturnTypePattern;
use Tempest\Highlight\Languages\DocComment\Patterns\DocCommentTagPattern;
use Tempest\Highlight\Languages\DocComment\Patterns\DocCommentTemplateTypePattern;
use Tempest\Highlight\Languages\DocComment\Patterns\DocCommentVariablePattern;
use Tempest\Highlight\Languages\DocComment\Patterns\DocCommentVarTypePattern;
use Tempest\Highlight\Languages\Php\Injections\PhpGenericTypeInjection;

class DocCommentLanguage extends BaseLanguage
{
public function getInjections(): array
{
return [
...parent::getInjections(),
new GenericTypeInjection(),
new PhpGenericTypeInjection(),
];
}

Expand All @@ -29,12 +23,6 @@ public function getPatterns(): array
return [
...parent::getPatterns(),
new DocCommentTagPattern(),
new DocCommentParamTypePattern(),
new DocCommentVarTypePattern(),
new DocCommentReturnTypePattern(),
new DocCommentTemplateTypePattern(),
new DocCommentGenericTypePattern(),
new DocCommentVariablePattern(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

declare(strict_types=1);

namespace Tempest\Highlight\Languages\DocComment\Injections;
namespace Tempest\Highlight\Languages\Php\Injections;

use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Injection;
use Tempest\Highlight\IsInjection;
use Tempest\Highlight\Languages\Php\PhpDocCommentLanguage;

final readonly class DocCommentInjection implements Injection
final readonly class PhpDocCommentInjection implements Injection
{
use IsInjection;

Expand All @@ -19,6 +20,6 @@ public function getPattern(): string

public function parseContent(string $content, Highlighter $highlighter): string
{
return $highlighter->parse($content, 'doc');
return $highlighter->parse($content, new PhpDocCommentLanguage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

namespace Tempest\Highlight\Languages\DocComment\Injections;
namespace Tempest\Highlight\Languages\Php\Injections;

use Tempest\Highlight\Escape;
use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Injection;
use Tempest\Highlight\Tokens\TokenTypeEnum;

final readonly class GenericTypeInjection implements Injection
final readonly class PhpGenericTypeInjection implements Injection
{
public function parse(string $content, Highlighter $highlighter): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

declare(strict_types=1);

namespace Tempest\Highlight\Languages\DocComment\Patterns;
namespace Tempest\Highlight\Languages\Php\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenTypeEnum;

#[PatternTest(input: '@param class-string<T> $className', output: 'T')]
final readonly class DocCommentGenericTypePattern implements Pattern
final readonly class PhpDocCommentGenericTypePattern implements Pattern
{
use IsPattern;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Highlight\Languages\DocComment\Patterns;
namespace Tempest\Highlight\Languages\Php\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
Expand All @@ -11,7 +11,7 @@

#[PatternTest(input: '@param array|string $concrete', output: 'array|string')]
#[PatternTest(input: '@param \\Foo $concrete', output: '\\Foo')]
final readonly class DocCommentParamTypePattern implements Pattern
final readonly class PhpDocCommentParamTypePattern implements Pattern
{
use IsPattern;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Highlight\Languages\DocComment\Patterns;
namespace Tempest\Highlight\Languages\Php\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
Expand All @@ -11,7 +11,7 @@

#[PatternTest(input: '@return array|string', output: 'array|string')]
#[PatternTest(input: '@return \\Foo', output: '\\Foo')]
final readonly class DocCommentReturnTypePattern implements Pattern
final readonly class PhpDocCommentReturnTypePattern implements Pattern
{
use IsPattern;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

declare(strict_types=1);

namespace Tempest\Highlight\Languages\DocComment\Patterns;
namespace Tempest\Highlight\Languages\Php\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenTypeEnum;

#[PatternTest(input: '@template T', output: 'T')]
final readonly class DocCommentTemplateTypePattern implements Pattern
final readonly class PhpDocCommentTemplateTypePattern implements Pattern
{
use IsPattern;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Highlight\Languages\DocComment\Patterns;
namespace Tempest\Highlight\Languages\Php\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
Expand All @@ -11,7 +11,7 @@

#[PatternTest(input: '@var array|string $concrete', output: 'array|string')]
#[PatternTest(input: '@var \\Foo $concrete', output: '\\Foo')]
final readonly class DocCommentVarTypePattern implements Pattern
final readonly class PhpDocCommentVarTypePattern implements Pattern
{
use IsPattern;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Highlight\Languages\DocComment\Patterns;
namespace Tempest\Highlight\Languages\Php\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
Expand All @@ -13,7 +13,7 @@
#[PatternTest(input: '@param $concrete', output: '$concrete')]
#[PatternTest(input: '@var Foo $concrete', output: '$concrete')]
#[PatternTest(input: '@var $concrete', output: '$concrete')]
final readonly class DocCommentVariablePattern implements Pattern
final readonly class PhpDocCommentVariablePattern implements Pattern
{
use IsPattern;

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

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Php;

use Tempest\Highlight\Languages\DocComment\DocCommentLanguage;
use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentGenericTypePattern;
use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentParamTypePattern;
use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentReturnTypePattern;
use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentTemplateTypePattern;
use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentVariablePattern;
use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentVarTypePattern;

class PhpDocCommentLanguage extends DocCommentLanguage
{
public function getPatterns(): array
{
return [
...parent::getPatterns(),
new PhpDocCommentParamTypePattern(),
new PhpDocCommentVarTypePattern(),
new PhpDocCommentReturnTypePattern(),
new PhpDocCommentTemplateTypePattern(),
new PhpDocCommentGenericTypePattern(),
new PhpDocCommentVariablePattern(),
];
}
}
4 changes: 2 additions & 2 deletions src/Languages/Php/PhpLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Tempest\Highlight\Languages\Php;

use Tempest\Highlight\Languages\Base\BaseLanguage;
use Tempest\Highlight\Languages\DocComment\Injections\DocCommentInjection;
use Tempest\Highlight\Languages\Php\Injections\PhpAttributeInjection;
use Tempest\Highlight\Languages\Php\Injections\PhpDocCommentInjection;
use Tempest\Highlight\Languages\Php\Injections\PhpFunctionParametersInjection;
use Tempest\Highlight\Languages\Php\Injections\PhpHeredocInjection;
use Tempest\Highlight\Languages\Php\Patterns\AttributeTypePattern;
Expand Down Expand Up @@ -52,7 +52,7 @@ public function getInjections(): array
return [
...parent::getInjections(),
new PhpHeredocInjection(),
new DocCommentInjection(),
new PhpDocCommentInjection(),
new PhpAttributeInjection(),
new PhpFunctionParametersInjection(),
];
Expand Down
1 change: 0 additions & 1 deletion src/Themes/highlight-light-lite.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pre, code {
color: #000;
}

.hl-comment span,
.hl-comment {
color: #888888;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

declare(strict_types=1);

namespace Tempest\Highlight\Tests\Languages\DocComment;
namespace Tempest\Highlight\Tests\Languages\Php;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Languages\Php\PhpDocCommentLanguage;

class DocCommentLanguageTest extends TestCase
class PhpDocCommentLanguageTest extends TestCase
{
#[DataProvider('data')]
public function test_highlight(string $content, string $expected): void
Expand All @@ -17,7 +18,7 @@ public function test_highlight(string $content, string $expected): void

$this->assertSame(
$expected,
$highlighter->parse($content, 'doc'),
$highlighter->parse($content, new PhpDocCommentLanguage()),
);
}

Expand Down
21 changes: 10 additions & 11 deletions tests/targets/test.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
```php
final readonly class BookController
{
#[Post(uri: '/books/create')]
public function store(BookRequest $request): Response
{
$book = map($request)->to(Book::class)->save();

return response()
->redirect(uri([self::class, 'show'], id: $book->id));
}
}
/**
* This function will do some things
*
* @template T
* @param class-string<T> $className the class' name
* @param int $id
* @return T|null
*
* That's about all
*/
```

0 comments on commit b9739ac

Please sign in to comment.