diff --git a/src/Languages/Php/Patterns/ClassPropertyPattern.php b/src/Languages/Php/Patterns/ClassPropertyPattern.php index 33be594..5203bf7 100644 --- a/src/Languages/Php/Patterns/ClassPropertyPattern.php +++ b/src/Languages/Php/Patterns/ClassPropertyPattern.php @@ -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(.+?)) (?\\$[\w]+)'; + return '(public|private|protected)(\(set\))?(\s(.+?)) (?\\$[\w]+)'; } public function getTokenType(): TokenTypeEnum diff --git a/src/Languages/Php/Patterns/PhpAsymmetricPropertyPattern.php b/src/Languages/Php/Patterns/PhpAsymmetricPropertyPattern.php index dd28812..e29180a 100644 --- a/src/Languages/Php/Patterns/PhpAsymmetricPropertyPattern.php +++ b/src/Languages/Php/Patterns/PhpAsymmetricPropertyPattern.php @@ -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)\((?set|get)\)/'; + return '/(public|private|protected)\((?set)\)/'; } public function getTokenType(): TokenType diff --git a/src/Languages/Php/PhpTypeLanguage.php b/src/Languages/Php/PhpTypeLanguage.php index 1b5cf9b..d2773e0 100644 --- a/src/Languages/Php/PhpTypeLanguage.php +++ b/src/Languages/Php/PhpTypeLanguage.php @@ -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; @@ -53,6 +54,7 @@ public function getPatterns(): array new TypeForVariablePattern(), new ClassPropertyPattern(), new NewObjectPattern(), + new PhpAsymmetricPropertyPattern(), ]; } } diff --git a/tests/targets/test.md b/tests/targets/test.md index 6198de4..b84f335 100644 --- a/tests/targets/test.md +++ b/tests/targets/test.md @@ -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, +) {} ``` \ No newline at end of file