diff --git a/src/Languages/Php/Patterns/ClassResolutionPattern.php b/src/Languages/Php/Patterns/ClassResolutionPattern.php new file mode 100644 index 0000000..bd58ac4 --- /dev/null +++ b/src/Languages/Php/Patterns/ClassResolutionPattern.php @@ -0,0 +1,26 @@ +class)'; + } + + public function getTokenType(): TokenType + { + return TokenType::KEYWORD; + } +} diff --git a/src/Languages/Php/Patterns/StaticClassCallPattern.php b/src/Languages/Php/Patterns/StaticClassCallPattern.php index a22e158..43f2e54 100644 --- a/src/Languages/Php/Patterns/StaticClassCallPattern.php +++ b/src/Languages/Php/Patterns/StaticClassCallPattern.php @@ -6,15 +6,18 @@ use Tempest\Highlight\IsPattern; use Tempest\Highlight\Pattern; +use Tempest\Highlight\PatternTest; use Tempest\Highlight\Tokens\TokenType; +#[PatternTest(input: 'Foo::bar()', output: 'Foo')] +#[PatternTest(input: 'Foo::BAR', output: 'Foo')] final readonly class StaticClassCallPattern implements Pattern { use IsPattern; public function getPattern(): string { - return '(?[\w]+)\:\:'; + return '(?[\w]+)\:\:'; } public function getTokenType(): TokenType diff --git a/src/Languages/Php/Patterns/VariablePattern.php b/src/Languages/Php/Patterns/VariablePattern.php new file mode 100644 index 0000000..5d577ad --- /dev/null +++ b/src/Languages/Php/Patterns/VariablePattern.php @@ -0,0 +1,26 @@ +\\$[\w]+)'; + } + + public function getTokenType(): TokenType + { + return TokenType::VARIABLE; + } +} diff --git a/src/Languages/Php/PhpLanguage.php b/src/Languages/Php/PhpLanguage.php index 979bf0a..b122cc2 100644 --- a/src/Languages/Php/PhpLanguage.php +++ b/src/Languages/Php/PhpLanguage.php @@ -10,6 +10,7 @@ use Tempest\Highlight\Languages\Php\Patterns\AttributeTypePattern; use Tempest\Highlight\Languages\Php\Patterns\ClassNamePattern; use Tempest\Highlight\Languages\Php\Patterns\ClassPropertyPattern; +use Tempest\Highlight\Languages\Php\Patterns\ClassResolutionPattern; use Tempest\Highlight\Languages\Php\Patterns\ConstantNamePattern; use Tempest\Highlight\Languages\Php\Patterns\ConstantPropertyPattern; use Tempest\Highlight\Languages\Php\Patterns\ConstantTypesPattern; @@ -36,6 +37,7 @@ use Tempest\Highlight\Languages\Php\Patterns\UntypedClassPropertyPattern; use Tempest\Highlight\Languages\Php\Patterns\UseAsPattern; use Tempest\Highlight\Languages\Php\Patterns\UsePattern; +use Tempest\Highlight\Languages\Php\Patterns\VariablePattern; class PhpLanguage extends BaseLanguage { @@ -125,6 +127,7 @@ public function getPatterns(): array new KeywordPattern('xor'), new KeywordPattern('yield'), new KeywordPattern('yield from'), + new ClassResolutionPattern(), // ATTRIBUTES new AttributePattern(), @@ -161,6 +164,9 @@ public function getPatterns(): array new ConstantNamePattern(), new UntypedClassPropertyPattern(), + // VARIABLES + new VariablePattern(), + // VALUES new SingleQuoteValuePattern(), new DoubleQuoteValuePattern(), diff --git a/src/Themes/CssTheme.php b/src/Themes/CssTheme.php index 7e10236..587f663 100644 --- a/src/Themes/CssTheme.php +++ b/src/Themes/CssTheme.php @@ -20,6 +20,7 @@ public function before(string|TokenType $tokenType): string TokenType::COMMENT => 'hl-comment', TokenType::ATTRIBUTE => 'hl-attribute', TokenType::INJECTION => 'hl-injection', + TokenType::VARIABLE => 'hl-variable', default => $tokenType, }; diff --git a/src/Tokens/TokenType.php b/src/Tokens/TokenType.php index 4e8705c..10faa36 100644 --- a/src/Tokens/TokenType.php +++ b/src/Tokens/TokenType.php @@ -12,13 +12,14 @@ enum TokenType: string case TYPE = 'type'; case GENERIC = 'generic'; case VALUE = 'value'; + case VARIABLE = 'variable'; case COMMENT = 'comment'; case INJECTION = 'injection'; public function canContain(self $other): bool { return match ($this) { - self::VALUE, self::INJECTION, self::COMMENT => false, + self::VARIABLE, self::VALUE, self::INJECTION, self::COMMENT => false, default => true, }; } diff --git a/tests/Languages/Blade/Injections/BladeEchoInjectionTest.php b/tests/Languages/Blade/Injections/BladeEchoInjectionTest.php index cb83200..730d8d4 100644 --- a/tests/Languages/Blade/Injections/BladeEchoInjectionTest.php +++ b/tests/Languages/Blade/Injections/BladeEchoInjectionTest.php @@ -19,7 +19,7 @@ public function test_injection(): void $this->assertMatches( injection: new BladeEchoInjection(), content: '{{ count($foo) }}', - expected: '{{ count($foo) }}', + expected: '{{ count($foo) }}', ); } } diff --git a/tests/Languages/Blade/Injections/BladeRawEchoInjectionTest.php b/tests/Languages/Blade/Injections/BladeRawEchoInjectionTest.php index 174b4bd..3fe37c5 100644 --- a/tests/Languages/Blade/Injections/BladeRawEchoInjectionTest.php +++ b/tests/Languages/Blade/Injections/BladeRawEchoInjectionTest.php @@ -19,7 +19,7 @@ public function test_injection_raw_echo(): void $this->assertMatches( injection: new BladeRawEchoInjection(), content: '{!! count($foo) !!}', - expected: '{!! count($foo) !!}', + expected: '{!! count($foo) !!}', ); } } diff --git a/tests/Languages/Php/Injections/PhpInjectionTest.php b/tests/Languages/Php/Injections/PhpInjectionTest.php index 7c32d49..a5f622f 100644 --- a/tests/Languages/Php/Injections/PhpInjectionTest.php +++ b/tests/Languages/Php/Injections/PhpInjectionTest.php @@ -28,7 +28,7 @@ public function test_injection(): void $expected = ' <?php /** @var \Tempest\View\GenericView $this */ - $var = new Foo(); + $var = new Foo(); ?> Hello, <?= $this->name ?> diff --git a/tests/Languages/Php/Injections/PhpShortEchoInjectionTest.php b/tests/Languages/Php/Injections/PhpShortEchoInjectionTest.php index 86386a2..061639d 100644 --- a/tests/Languages/Php/Injections/PhpShortEchoInjectionTest.php +++ b/tests/Languages/Php/Injections/PhpShortEchoInjectionTest.php @@ -22,8 +22,8 @@ public function test_injection(): void '; $expected = ' -Hello, <?= $this->name ?> -Hello, <?= $this->other ?> +Hello, <?= $this->name ?> +Hello, <?= $this->other ?> '; $this->assertMatches( diff --git a/tests/Languages/Php/Patterns/StaticClassCallPatternTest.php b/tests/Languages/Php/Patterns/StaticClassCallPatternTest.php deleted file mode 100644 index b38fb52..0000000 --- a/tests/Languages/Php/Patterns/StaticClassCallPatternTest.php +++ /dev/null @@ -1,29 +0,0 @@ -assertMatches( - pattern: new StaticClassCallPattern(), - content: 'Foo::bar()', - expected: 'Foo', - ); - - $this->assertMatches( - pattern: new StaticClassCallPattern(), - content: 'Foo::BAR', - expected: 'Foo', - ); - } -} diff --git a/tests/Languages/Php/PhpLanguageTest.php b/tests/Languages/Php/PhpLanguageTest.php index 938197d..c740b0d 100644 --- a/tests/Languages/Php/PhpLanguageTest.php +++ b/tests/Languages/Php/PhpLanguageTest.php @@ -27,14 +27,15 @@ public static function data(): array ["'php()'", "'php()'"], ["public const string|\Stringable MESSAGE = 'hi';", 'public const string|\Stringable MESSAGE = \'hi\';'], ["public string|\Stringable \$message;", 'public string|\Stringable $message;'], - ['for($x = 0; $x < 150; $x++) {', 'for($x = 0; $x < 150; $x++) {'], + ['for($x = 0; $x < 150; $x++) {', 'for($x = 0; $x < 150; $x++) {'], ["'namespace ';", "'namespace ';"], - ['$class', '$class'], + ['$class', '$class'], ['protected $resolved = [];', 'protected $resolved = [];'], ['protected Foo $resolved = [];', 'protected Foo $resolved = [];'], - ['$concrete instanceof Closure', '$concrete instanceof Closure'], + ['$concrete instanceof Closure', '$concrete instanceof Closure'], ['extends Foo implements ArrayAccess, ContainerContract', 'extends Foo implements ArrayAccess, ContainerContract'], ['use Illuminate\Contracts\Container\Container as ContainerContract', 'use Illuminate\Contracts\Container\Container as ContainerContract'], + ['$foo::class;', '$foo::class;'], [ "// We'll diff --git a/tests/stubs/02.txt b/tests/stubs/02.txt index 2dc5ec2..01d6da4 100644 --- a/tests/stubs/02.txt +++ b/tests/stubs/02.txt @@ -17,13 +17,13 @@ <body> <div> <?php - $sql = <<<SQL + $sql = <<<SQL SELECT posts.*, authors.name FROM posts INNER JOIN authors ON posts.author_id = authors.id SQL; - $posts = DB::get($sql); + $posts = DB::get($sql); // Don't do this IRL :) ?> diff --git a/tests/test.md b/tests/test.md index fc0f04a..fd336e3 100644 --- a/tests/test.md +++ b/tests/test.md @@ -1,3 +1,3 @@ ```php -use Illuminate\Contracts\Container\Container as ContainerContract +$foo::class; ``` \ No newline at end of file