Skip to content

Commit

Permalink
Asymmetric visibility updates
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Nov 13, 2024
1 parent 099967d commit 2f19fab
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
5 changes: 4 additions & 1 deletion src/Languages/Php/Patterns/ClassPropertyPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
#[PatternTest(input: 'public Foo|Bar $foo', output: '$foo')]
#[PatternTest(input: 'public Foo&Bar $foo', output: '$foo')]
#[PatternTest(input: 'public (Foo&Bar)|null $foo', output: '$foo')]
#[PatternTest(input: 'private(set) Foo $foo;', output: '$foo')]
#[PatternTest(input: 'public(set) Foo $foo;', output: '$foo')]
#[PatternTest(input: 'protected(set) Foo $foo;', output: '$foo')]
final readonly class ClassPropertyPattern implements Pattern
{
use IsPattern;

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

public function getTokenType(): TokenTypeEnum
Expand Down
20 changes: 16 additions & 4 deletions src/Languages/Php/Patterns/PhpAsymmetricPropertyPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,28 @@
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')]
#[PatternTest(input: 'public private(set) Foo $foo', output: 'set')]
#[PatternTest(input: 'public protected(set) Foo $foo', output: 'set')]
final readonly class PhpAsymmetricPropertyPattern implements Pattern
{
use IsPattern;

public function match(string $content): array
{
$pattern = $this->getPattern();

if (! str_starts_with($pattern, '/')) {
$pattern = "/$pattern/";
}

preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE);

return $matches;
}

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

public function getTokenType(): TokenType
Expand Down
2 changes: 2 additions & 0 deletions src/Languages/Php/PhpTypeLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Tempest\Highlight\Languages\Php\Patterns\KeywordPattern;
use Tempest\Highlight\Languages\Php\Patterns\MultilineSingleDocCommentPattern;
use Tempest\Highlight\Languages\Php\Patterns\NewObjectPattern;
use Tempest\Highlight\Languages\Php\Patterns\PhpAsymmetricPropertyPattern;
use Tempest\Highlight\Languages\Php\Patterns\SinglelineCommentPattern;
use Tempest\Highlight\Languages\Php\Patterns\TypeForVariablePattern;

Expand Down Expand Up @@ -53,6 +54,7 @@ public function getPatterns(): array
new TypeForVariablePattern(),
new ClassPropertyPattern(),
new NewObjectPattern(),
new PhpAsymmetricPropertyPattern(),
];
}
}
21 changes: 3 additions & 18 deletions tests/targets/test.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
```php
// 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
{
}
}
public function __construct(
private(set) Author $author,
) {}
```

0 comments on commit 2f19fab

Please sign in to comment.