diff --git a/UPGRADING.md b/UPGRADING.md index c995f42..21a24f1 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -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 diff --git a/src/Languages/Php/Patterns/PropertyHookGetPattern.php b/src/Languages/Php/Patterns/PropertyHookGetPattern.php new file mode 100644 index 0000000..f1a6d2f --- /dev/null +++ b/src/Languages/Php/Patterns/PropertyHookGetPattern.php @@ -0,0 +1,37 @@ +', + 'get' +)] +#[PatternTest('get;', 'get')] +final readonly class PropertyHookGetPattern implements Pattern +{ + use IsPattern; + + public function getPattern(): string + { + return '(?get)\s*({|=>|;)'; + } + + public function getTokenType(): TokenType + { + return TokenTypeEnum::KEYWORD; + } +} diff --git a/src/Languages/Php/Patterns/PropertyHookSetParameterTypePattern.php b/src/Languages/Php/Patterns/PropertyHookSetParameterTypePattern.php new file mode 100644 index 0000000..abd7f2a --- /dev/null +++ b/src/Languages/Php/Patterns/PropertyHookSetParameterTypePattern.php @@ -0,0 +1,28 @@ +string)'; + } + + public function getTokenType(): TokenType + { + return TokenTypeEnum::TYPE; + } +} diff --git a/src/Languages/Php/Patterns/PropertyHookSetPattern.php b/src/Languages/Php/Patterns/PropertyHookSetPattern.php new file mode 100644 index 0000000..2905c5d --- /dev/null +++ b/src/Languages/Php/Patterns/PropertyHookSetPattern.php @@ -0,0 +1,35 @@ +', 'set')] +#[PatternTest('set (string $value', 'set')] +#[PatternTest('set(string $value', 'set')] +final readonly class PropertyHookSetPattern implements Pattern +{ + use IsPattern; + + public function getPattern(): string + { + return '(?set)\s*({|;|=>|\()'; + } + + public function getTokenType(): TokenType + { + return TokenTypeEnum::KEYWORD; + } +} diff --git a/src/Languages/Php/PhpLanguage.php b/src/Languages/Php/PhpLanguage.php index 6feaf2b..6b48f33 100644 --- a/src/Languages/Php/PhpLanguage.php +++ b/src/Languages/Php/PhpLanguage.php @@ -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; @@ -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'), @@ -149,6 +153,8 @@ public function getPatterns(): array new KeywordPattern('yield from'), new ClassResolutionPattern(), new ShortFunctionReferencePattern(), + new PropertyHookSetPattern(), + new PropertyHookGetPattern(), // COMMENTS new MultilineSingleDocCommentPattern(), @@ -171,6 +177,7 @@ public function getPatterns(): array new CatchTypePattern(), new EnumBackedTypePattern(), new GroupedTypePattern(), + new PropertyHookSetParameterTypePattern(), // PROPERTIES new ClassPropertyPattern(), diff --git a/tests/Languages/Php/PhpLanguageTest.php b/tests/Languages/Php/PhpLanguageTest.php index ef44831..b0bf4e0 100644 --- a/tests/Languages/Php/PhpLanguageTest.php +++ b/tests/Languages/Php/PhpLanguageTest.php @@ -57,6 +57,16 @@ public static function data(): array ['/** @var Foo $var */', '/** @var Foo $var */'], ['{~}): Foo {}~}', '}): Foo {}'], ['{~class~} Foo {}', 'class Foo {}'], + [ + 'public string $fullName { + get => $this->first . " " . $this->last; + set (string $value) => $this->first . " " . $this->last; + }', + 'public string $fullName { + get => $this->first . " " . $this->last; + set (string $value) => $this->first . " " . $this->last; + }', + ], [ "// We'll diff --git a/tests/index.php b/tests/index.php index 9901430..c6bef93 100644 --- a/tests/index.php +++ b/tests/index.php @@ -14,7 +14,7 @@ $environment = new Environment(); -$highlighter = (new Highlighter(new CssTheme()))->withGutter(); +$highlighter = (new Highlighter(new CssTheme())); $environment ->addExtension(new CommonMarkCoreExtension()) diff --git a/tests/targets/test.md b/tests/targets/test.md index 16a567b..b4521a5 100644 --- a/tests/targets/test.md +++ b/tests/targets/test.md @@ -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; +} ``` \ No newline at end of file