Skip to content

Commit

Permalink
Support property hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Oct 24, 2024
1 parent dc2e025 commit 9e0c13c
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
5 changes: 5 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.10.6

- Support for property hooks
- Include quotes in PHP and JS strings

## 2.10.3

- Normalize newline formats (#150)
Expand Down
28 changes: 28 additions & 0 deletions src/Languages/Php/Patterns/PhpAsymmetricPropertyPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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(input: 'public public(set) Foo $foo', output: 'set')]
#[PatternTest(input: 'public public(get) Foo $foo', output: 'get')]
#[PatternTest(input: 'public private(get) Foo $foo', output: 'get')]
#[PatternTest(input: 'public protected(get) Foo $foo', output: 'get')]
final readonly class PhpAsymmetricPropertyPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '/(public|private|protected)\((?<match>set|get)\)/';
}

public function getTokenType(): TokenType
{
return TokenTypeEnum::KEYWORD;
}
}
6 changes: 5 additions & 1 deletion src/Languages/Php/Patterns/PropertyTypesPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#[PatternTest(input: 'public (Bar&Baz)|null $bar', output: '(Bar&Baz)|null')]
#[PatternTest(input: 'public ?Bar $bar', output: '?Bar')]
#[PatternTest(input: 'public ?Bar|\Foo $bar', output: '?Bar|\Foo')]
#[PatternTest(input: 'public private(set) ?Bar|\Foo $bar', output: '?Bar|\Foo')]
#[PatternTest(input: 'public private(set) Foo $foo', output: 'Foo')]
#[PatternTest(input: 'public public(set) Foo $foo', output: 'Foo')]
#[PatternTest(input: 'public protected(set) Foo $foo', output: 'Foo')]
#[PatternTest(input: 'public function bar(mixed $input);', output: null)]
#[PatternTest(input: 'private static ?Highlighter $web = null;', output: '?Highlighter')]
final readonly class PropertyTypesPattern implements Pattern
Expand All @@ -25,7 +29,7 @@

public function getPattern(): string
{
return '(public|private|protected)(\s*static\s*)?(\s(?<match>[^\s]*)) (\$[\w]+)';
return '(public|private|protected)(\s*static\s*)?(\s*\((get|set)\)\s*)?(\s(?<match>[^\s]*)) (\$[\w]+)';
}

public function getTokenType(): TokenTypeEnum
Expand Down
2 changes: 2 additions & 0 deletions src/Languages/Php/PhpLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Tempest\Highlight\Languages\Php\Patterns\NestedFunctionCallPattern;
use Tempest\Highlight\Languages\Php\Patterns\NewObjectPattern;
use Tempest\Highlight\Languages\Php\Patterns\OperatorPattern;
use Tempest\Highlight\Languages\Php\Patterns\PhpAsymmetricPropertyPattern;
use Tempest\Highlight\Languages\Php\Patterns\PhpCloseTagPattern;
use Tempest\Highlight\Languages\Php\Patterns\PhpOpenTagPattern;
use Tempest\Highlight\Languages\Php\Patterns\PropertyAccessPattern;
Expand Down Expand Up @@ -91,6 +92,7 @@ public function getPatterns(): array
new FunctionNamePattern(),

// KEYWORDS
new PhpAsymmetricPropertyPattern(),
new KeywordPattern('null'),
new KeywordPattern('parent'),
new KeywordPattern('true'),
Expand Down
1 change: 1 addition & 0 deletions tests/Languages/Php/PhpLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static function data(): array
{
return [
["'php()'", "<span class=\"hl-value\">'php()'</span>"],
['public private(set) Author $author,', '<span class="hl-keyword">public</span> <span class="hl-keyword">private</span>(<span class="hl-keyword">set</span>) <span class="hl-type">Author</span> <span class="hl-property">$author</span>,'],
["#[PatternTest(input: 'new Foo()', output: 'Foo')]", '<span class="hl-injection"><span class="hl-attribute">#[<span class="hl-type">PatternTest</span>(<span class="hl-property">input</span>: <span class="hl-value">\'new Foo()\'</span>, <span class="hl-property">output</span>: <span class="hl-value">\'Foo\'</span>)]</span></span>'],
['$foo && $bar', '<span class="hl-variable">$foo</span> <span class="hl-operator">&amp;&amp;</span> <span class="hl-variable">$bar</span>'],
['$foo || $bar', '<span class="hl-variable">$foo</span> <span class="hl-operator">||</span> <span class="hl-variable">$bar</span>'],
Expand Down
8 changes: 4 additions & 4 deletions tests/stubs/03.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ final readonly class HomeController
<span class="hl-comment">// controller for home</span>
<span class="hl-keyword">final</span> <span class="hl-keyword">readonly</span> <span class="hl-keyword">class</span> <span class="hl-type">HomeController</span>
{
<span class="hl-injection"><span class="hl-attribute">#[<span class="hl-type">Get</span>(<span class="hl-property">uri</span>: '<span class="hl-value">/home</span>')]</span></span>
<span class="hl-injection"><span class="hl-attribute">#[<span class="hl-type">Get</span>(<span class="hl-property">uri</span>: <span class="hl-value">'/home'</span>)]</span></span>
<span class="hl-keyword">public</span> <span class="hl-keyword">function</span> <span class="hl-property">__invoke</span>(): <span class="hl-type">View</span>
{
<span class="hl-keyword">return</span> <span class="hl-property">view</span>('<span class="hl-value">Views/home.view.php</span>')
<span class="hl-keyword">return</span> <span class="hl-property">view</span>(<span class="hl-value">'Views/home.view.php'</span>)
-&gt;<span class="hl-property">data</span>(
<span class="hl-property">name</span>: '<span class="hl-value">Brent</span>',
<span class="hl-property">name</span>: <span class="hl-value">'Brent'</span>,
<span class="hl-property">date</span>: <span class="hl-keyword">new</span> <span class="hl-type">DateTime</span>(),
);
}

<span class="hl-injection"><span class="hl-attribute">#[<span class="hl-type">Post</span>(<span class="hl-property">uri</span>: '<span class="hl-value">/home</span>')]</span></span>
<span class="hl-injection"><span class="hl-attribute">#[<span class="hl-type">Post</span>(<span class="hl-property">uri</span>: <span class="hl-value">'/home'</span>)]</span></span>
<span class="hl-keyword">public</span> <span class="hl-keyword">function</span> <span class="hl-property">__invoke</span>(): <span class="hl-type">View</span>
{
}
Expand Down
19 changes: 18 additions & 1 deletion tests/targets/test.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
```php
public static function new(mixed ...$params): self;
// controller for home
final readonly class HomeController
{
#[Get(uri: '/home')]
public function __invoke(): View
{
return view('Views/home.view.php')
->data(
name: 'Brent',
date: new DateTime(),
);
}

#[Post(uri: '/home')]
public function __invoke(): View
{
}
}
```

0 comments on commit 9e0c13c

Please sign in to comment.