Skip to content

Commit 9e0c13c

Browse files
committed
Support property hooks
1 parent dc2e025 commit 9e0c13c

File tree

7 files changed

+63
-6
lines changed

7 files changed

+63
-6
lines changed

UPGRADING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.10.6
2+
3+
- Support for property hooks
4+
- Include quotes in PHP and JS strings
5+
16
## 2.10.3
27

38
- Normalize newline formats (#150)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tempest\Highlight\Languages\Php\Patterns;
4+
5+
use Tempest\Highlight\IsPattern;
6+
use Tempest\Highlight\Pattern;
7+
use Tempest\Highlight\PatternTest;
8+
use Tempest\Highlight\Tokens\TokenType;
9+
use Tempest\Highlight\Tokens\TokenTypeEnum;
10+
11+
#[PatternTest(input: 'public public(set) Foo $foo', output: 'set')]
12+
#[PatternTest(input: 'public public(get) Foo $foo', output: 'get')]
13+
#[PatternTest(input: 'public private(get) Foo $foo', output: 'get')]
14+
#[PatternTest(input: 'public protected(get) Foo $foo', output: 'get')]
15+
final readonly class PhpAsymmetricPropertyPattern implements Pattern
16+
{
17+
use IsPattern;
18+
19+
public function getPattern(): string
20+
{
21+
return '/(public|private|protected)\((?<match>set|get)\)/';
22+
}
23+
24+
public function getTokenType(): TokenType
25+
{
26+
return TokenTypeEnum::KEYWORD;
27+
}
28+
}

src/Languages/Php/Patterns/PropertyTypesPattern.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#[PatternTest(input: 'public (Bar&Baz)|null $bar', output: '(Bar&Baz)|null')]
1818
#[PatternTest(input: 'public ?Bar $bar', output: '?Bar')]
1919
#[PatternTest(input: 'public ?Bar|\Foo $bar', output: '?Bar|\Foo')]
20+
#[PatternTest(input: 'public private(set) ?Bar|\Foo $bar', output: '?Bar|\Foo')]
21+
#[PatternTest(input: 'public private(set) Foo $foo', output: 'Foo')]
22+
#[PatternTest(input: 'public public(set) Foo $foo', output: 'Foo')]
23+
#[PatternTest(input: 'public protected(set) Foo $foo', output: 'Foo')]
2024
#[PatternTest(input: 'public function bar(mixed $input);', output: null)]
2125
#[PatternTest(input: 'private static ?Highlighter $web = null;', output: '?Highlighter')]
2226
final readonly class PropertyTypesPattern implements Pattern
@@ -25,7 +29,7 @@
2529

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

3135
public function getTokenType(): TokenTypeEnum

src/Languages/Php/PhpLanguage.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Tempest\Highlight\Languages\Php\Patterns\NestedFunctionCallPattern;
3535
use Tempest\Highlight\Languages\Php\Patterns\NewObjectPattern;
3636
use Tempest\Highlight\Languages\Php\Patterns\OperatorPattern;
37+
use Tempest\Highlight\Languages\Php\Patterns\PhpAsymmetricPropertyPattern;
3738
use Tempest\Highlight\Languages\Php\Patterns\PhpCloseTagPattern;
3839
use Tempest\Highlight\Languages\Php\Patterns\PhpOpenTagPattern;
3940
use Tempest\Highlight\Languages\Php\Patterns\PropertyAccessPattern;
@@ -91,6 +92,7 @@ public function getPatterns(): array
9192
new FunctionNamePattern(),
9293

9394
// KEYWORDS
95+
new PhpAsymmetricPropertyPattern(),
9496
new KeywordPattern('null'),
9597
new KeywordPattern('parent'),
9698
new KeywordPattern('true'),

tests/Languages/Php/PhpLanguageTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static function data(): array
2525
{
2626
return [
2727
["'php()'", "<span class=\"hl-value\">'php()'</span>"],
28+
['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>,'],
2829
["#[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>'],
2930
['$foo && $bar', '<span class="hl-variable">$foo</span> <span class="hl-operator">&amp;&amp;</span> <span class="hl-variable">$bar</span>'],
3031
['$foo || $bar', '<span class="hl-variable">$foo</span> <span class="hl-operator">||</span> <span class="hl-variable">$bar</span>'],

tests/stubs/03.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ final readonly class HomeController
2020
<span class="hl-comment">// controller for home</span>
2121
<span class="hl-keyword">final</span> <span class="hl-keyword">readonly</span> <span class="hl-keyword">class</span> <span class="hl-type">HomeController</span>
2222
{
23-
<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>
23+
<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>
2424
<span class="hl-keyword">public</span> <span class="hl-keyword">function</span> <span class="hl-property">__invoke</span>(): <span class="hl-type">View</span>
2525
{
26-
<span class="hl-keyword">return</span> <span class="hl-property">view</span>('<span class="hl-value">Views/home.view.php</span>')
26+
<span class="hl-keyword">return</span> <span class="hl-property">view</span>(<span class="hl-value">'Views/home.view.php'</span>)
2727
-&gt;<span class="hl-property">data</span>(
28-
<span class="hl-property">name</span>: '<span class="hl-value">Brent</span>',
28+
<span class="hl-property">name</span>: <span class="hl-value">'Brent'</span>,
2929
<span class="hl-property">date</span>: <span class="hl-keyword">new</span> <span class="hl-type">DateTime</span>(),
3030
);
3131
}
3232

33-
<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>
33+
<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>
3434
<span class="hl-keyword">public</span> <span class="hl-keyword">function</span> <span class="hl-property">__invoke</span>(): <span class="hl-type">View</span>
3535
{
3636
}

tests/targets/test.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
```php
2-
public static function new(mixed ...$params): self;
2+
// controller for home
3+
final readonly class HomeController
4+
{
5+
#[Get(uri: '/home')]
6+
public function __invoke(): View
7+
{
8+
return view('Views/home.view.php')
9+
->data(
10+
name: 'Brent',
11+
date: new DateTime(),
12+
);
13+
}
14+
15+
#[Post(uri: '/home')]
16+
public function __invoke(): View
17+
{
18+
}
19+
}
320
```

0 commit comments

Comments
 (0)