Skip to content

Commit

Permalink
enh(php) add PHP constants
Browse files Browse the repository at this point in the history
  • Loading branch information
wkania committed Jan 2, 2022
1 parent c90d127 commit 0e30006
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/languages/php.js
Expand Up @@ -380,6 +380,18 @@ export default function(hljs) {
},
VARIABLE,
FUNCTION_INVOKE,
{
match: [
/const/,
regex.concat(WHITESPACE, "+"),
IDENT_RE,
regex.concat(WHITESPACE, "*="),
],
scope: {
1: "keyword",
3: "variable",
},
},
{
// swallow composed identifiers to avoid parsing them as keywords
match: regex.concat(
Expand Down
22 changes: 22 additions & 0 deletions test/markup/php/titles.expect.txt
@@ -0,0 +1,22 @@
<span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Example</span> </span>{
<span class="hljs-keyword">const</span> <span class="hljs-variable">FOO</span>=<span class="hljs-string">&#x27;foo&#x27;</span>;

<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span>(<span class="hljs-params">
<span class="hljs-keyword">public</span> <span class="hljs-keyword">readonly</span> <span class="hljs-keyword">string</span> <span class="hljs-variable">$name</span> = <span class="hljs-built_in">self</span>::<span class="hljs-variable">FOO</span>
</span>) </span>{}

<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getClass</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-title class_">DateTimeImmutable</span>::<span class="hljs-variable language_">class</span>;
}

<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getClassFromSelf</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-built_in">self</span>::<span class="hljs-variable language_">class</span>;
}

<span class="hljs-keyword">public</span> <span class="hljs-built_in">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getClassFromStatic</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-built_in">static</span>::<span class="hljs-variable language_">class</span>;
}
}

<span class="hljs-variable">$date</span> = DateTimeImmutable::<span class="hljs-title function_ invoke__">createFromMutable</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">\DateTime</span>());
<span class="hljs-keyword">echo</span> <span class="hljs-variable">$date</span>::<span class="hljs-variable language_">class</span>;
22 changes: 22 additions & 0 deletions test/markup/php/titles.txt
@@ -0,0 +1,22 @@
final class Example {
const FOO='foo';

public function __construct(
public readonly string $name = self::FOO
) {}

public function getClass(): string {
return DateTimeImmutable::class;
}

public function getClassFromSelf(): string {
return self::class;
}

public static function getClassFromStatic(): string {
return static::class;
}
}

$date = DateTimeImmutable::createFromMutable(new \DateTime());
echo $date::class;

0 comments on commit 0e30006

Please sign in to comment.