Skip to content

Commit

Permalink
Add property hooks support
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Apr 16, 2024
1 parent f0d415e commit 93d40fa
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 10 deletions.
1 change: 1 addition & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ interface Pattern
```

- `Highlighter::withGutter()` is now an immutable function
- Support for [PHP's property hooks](https://wiki.php.net/rfc/property-hooks)

## 1.3.4

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

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Php\Patterns;

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

#[PatternTest(
' public string $name {
get {',
'get'
)]
#[PatternTest(
' public string $name {
get =>',
'get'
)]
#[PatternTest('get;', 'get')]
final readonly class PropertyHookGetPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?<match>get)\s*({|=>|;)';
}

public function getTokenType(): TokenType
{
return TokenTypeEnum::KEYWORD;
}
}
28 changes: 28 additions & 0 deletions src/Languages/Php/Patterns/PropertyHookSetParameterTypePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Php\Patterns;

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

#[PatternTest('set (string $value', 'string')]
#[PatternTest('set(string $value', 'string')]
final readonly class PropertyHookSetParameterTypePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return 'set\s*\((?<match>string)';
}

public function getTokenType(): TokenType
{
return TokenTypeEnum::TYPE;
}
}
35 changes: 35 additions & 0 deletions src/Languages/Php/Patterns/PropertyHookSetPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Php\Patterns;

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

#[PatternTest(
' public string $name {
set {',
'set'
)]
#[PatternTest('set;', 'set')]
#[PatternTest('set =>', 'set')]
#[PatternTest('set (string $value', 'set')]
#[PatternTest('set(string $value', 'set')]
final readonly class PropertyHookSetPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?<match>set)\s*({|;|=>|\()';
}

public function getTokenType(): TokenType
{
return TokenTypeEnum::KEYWORD;
}
}
7 changes: 7 additions & 0 deletions src/Languages/Php/PhpLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
use Tempest\Highlight\Languages\Php\Patterns\NewObjectPattern;
use Tempest\Highlight\Languages\Php\Patterns\OperatorPattern;
use Tempest\Highlight\Languages\Php\Patterns\PropertyAccessPattern;
use Tempest\Highlight\Languages\Php\Patterns\PropertyHookGetPattern;
use Tempest\Highlight\Languages\Php\Patterns\PropertyHookSetParameterTypePattern;
use Tempest\Highlight\Languages\Php\Patterns\PropertyHookSetPattern;
use Tempest\Highlight\Languages\Php\Patterns\PropertyTypesPattern;
use Tempest\Highlight\Languages\Php\Patterns\ReturnTypePattern;
use Tempest\Highlight\Languages\Php\Patterns\ShortFunctionReferencePattern;
Expand Down Expand Up @@ -75,6 +78,7 @@ public function getPatterns(): array

// KEYWORDS
new KeywordPattern('null'),
new KeywordPattern('parent'),
new KeywordPattern('true'),
new KeywordPattern('false'),
new KeywordPattern('__halt_compiler'),
Expand Down Expand Up @@ -149,6 +153,8 @@ public function getPatterns(): array
new KeywordPattern('yield from'),
new ClassResolutionPattern(),
new ShortFunctionReferencePattern(),
new PropertyHookSetPattern(),
new PropertyHookGetPattern(),

// COMMENTS
new MultilineSingleDocCommentPattern(),
Expand All @@ -171,6 +177,7 @@ public function getPatterns(): array
new CatchTypePattern(),
new EnumBackedTypePattern(),
new GroupedTypePattern(),
new PropertyHookSetParameterTypePattern(),

// PROPERTIES
new ClassPropertyPattern(),
Expand Down
10 changes: 10 additions & 0 deletions tests/Languages/Php/PhpLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ public static function data(): array
['/** @var Foo $var */', '<span class="hl-comment">/** <span class="hl-value">@var</span> <span class="hl-type">Foo</span> <span class="hl-variable">$var</span> */</span>'],
['{~}): Foo {}~}', '<span class="hl-blur">}): <span class="hl-type">Foo</span> {}</span>'],
['{~class~} Foo {}', '<span class="hl-blur"><span class="hl-keyword">class</span></span> <span class="hl-type">Foo</span> {}'],
[
'public string $fullName {
get => $this->first . " " . $this->last;
set (string $value) => $this->first . " " . $this->last;
}',
'<span class="hl-keyword">public</span> <span class="hl-type">string</span> <span class="hl-property">$fullName</span> {
<span class="hl-keyword">get</span> =&gt; <span class="hl-variable">$this</span>-&gt;<span class="hl-property">first</span> . &quot;<span class="hl-value"> </span>&quot; . <span class="hl-variable">$this</span>-&gt;<span class="hl-property">last</span>;
<span class="hl-keyword">set</span> (<span class="hl-type">string</span> <span class="hl-variable">$value</span>) =&gt; <span class="hl-variable">$this</span>-&gt;<span class="hl-property">first</span> . &quot;<span class="hl-value"> </span>&quot; . <span class="hl-variable">$this</span>-&gt;<span class="hl-property">last</span>;
}',
],
[
"// We'll
Expand Down
2 changes: 1 addition & 1 deletion tests/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

$environment = new Environment();

$highlighter = (new Highlighter(new CssTheme()))->withGutter();
$highlighter = (new Highlighter(new CssTheme()));

$environment
->addExtension(new CommonMarkCoreExtension())
Expand Down
14 changes: 5 additions & 9 deletions tests/targets/test.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
```yaml
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
{+ pull_request_target: +}
{+ types: [opened, synchronize, reopened, ready_for_review] +}
{+ types: [opened, synchronize, reopened, ready_for_review] +}
{+ types: [opened, synchronize, reopened, ready_for_review] +}
other: foo
```php
public string $fullName {
get => $this->first . " " . $this->last;
set (string $value) => $this->first . " " . $this->last;
}
```

0 comments on commit 93d40fa

Please sign in to comment.