Skip to content

Commit

Permalink
enh(php) support class constructor call
Browse files Browse the repository at this point in the history
  • Loading branch information
wkania committed Dec 13, 2021
1 parent df6b1ac commit 5bd46df
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -13,6 +13,9 @@ These changes should be for the better and should not be super noticeable but if

Grammars:

- enh(php) support class constructor call (#3427) [Wojciech Kania][]
- enh(php) support function invoke (#3427) [Wojciech Kania][]
- enh(php) Switch highlighter to partially case-insensitive (#3427) [Wojciech Kania][]
- enh(php) improve `namespace` and `use` highlighting (#3427) [Josh Goebel][]
- enh(php) `$this` is a `variable.language` now (#3427) [Josh Goebel][]
- enh(php) add `__COMPILER_HALT_OFFSET__` (#3427) [Josh Goebel][]
Expand Down
30 changes: 24 additions & 6 deletions src/languages/php.js
Expand Up @@ -288,11 +288,31 @@ export default function(hljs) {
built_in: BUILT_INS.concat([ "Error|0" ]),
};

const CONSTRUCTOR_CALL = {
variants: [
{
match: [
/new/,
/\s+/,
// to prevent built ins from being confused as the class constructor call
regex.concat("(?!", BUILT_INS.join("\\b|"), "\\b)"),
/\\?\w+/,
/\s*\(/,
],
scope: {
1: "keyword",
4: "title.class",
},
}
]
};

const FUNCTION_INVOKE = {
relevance: 0,
match: [
/(?:->|::|\s|\(|\\)/,
regex.concat("(?!fn\\b|function\\b|match\\b|", KWS.join("\\b|"), "|", BUILT_INS.join("\\b|"), "\\b)"),
// to prevent keywords from being confused as the function title
regex.concat("(?!fn\\b|function\\b|match\\b|Error\\b|", KWS.join("\\b|"), "|", BUILT_INS.join("\\b|"), "\\b)"),
/\w+/,
/\s*/,
regex.lookahead(/(?=\()/)
Expand All @@ -301,6 +321,7 @@ export default function(hljs) {
3: "function.title.invoke",
}
};

return {
case_insensitive: false,
keywords: KEYWORDS,
Expand Down Expand Up @@ -343,12 +364,9 @@ export default function(hljs) {
FUNCTION_INVOKE,
{
// swallow composed identifiers to avoid parsing them as keywords
begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?!\()/
},
{
// swallow create object
begin: /new\s\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\s?\(/
begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/
},
CONSTRUCTOR_CALL,
{
className: 'function',
relevance: 0,
Expand Down
23 changes: 21 additions & 2 deletions test/markup/php/functions.expect.txt
Expand Up @@ -10,9 +10,28 @@
<span class="hljs-comment">/**
* Function invoke
*/</span>
<span class="hljs-variable">$date</span> = new DateTimeImmutable ();
<span class="hljs-variable">$date</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">DateTimeImmutable</span> ();
<span class="hljs-variable">$date</span>-&gt;<span class="hljs-function title_ invoke__">format</span>(<span class="hljs-string">&#x27;Y-m-d&#x27;</span>);

DateTimeImmutable::<span class="hljs-function title_ invoke__">createFromMutable</span>(new \DateTime());
DateTimeImmutable::<span class="hljs-function title_ invoke__">createFromMutable</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">\DateTime</span>(<span class="hljs-string">&#x27;now&#x27;</span>));

<span class="hljs-function title_ invoke__">str_contains</span> (\<span class="hljs-function title_ invoke__">strtoupper</span>(<span class="hljs-function title_ invoke__">substr</span>(<span class="hljs-string">&#x27;abcdef&#x27;</span>, -<span class="hljs-number">2</span>), <span class="hljs-string">&#x27;f&#x27;</span>));

<span class="hljs-comment">/**
* Function declaration
*/</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">testMe</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>|<span class="hljs-keyword">int</span> <span class="hljs-variable">$name</span></span>): <span class="hljs-title">int</span>
</span>{
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">empty</span>(<span class="hljs-variable">$name</span>)) {
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
} <span class="hljs-keyword">elseif</span> (<span class="hljs-variable">$name</span> === <span class="hljs-number">1</span>) {
<span class="hljs-keyword">return</span> (<span class="hljs-keyword">int</span>) <span class="hljs-variable">$name</span>;
}

<span class="hljs-keyword">switch</span>(<span class="hljs-variable">$name</span>) {
<span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;2&#x27;</span>:
<span class="hljs-keyword">return</span> <span class="hljs-number">2</span>;
<span class="hljs-keyword">default</span>:
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">\Exception</span>(<span class="hljs-string">&#x27;error&#x27;</span>);
}
}
21 changes: 20 additions & 1 deletion test/markup/php/functions.txt
Expand Up @@ -13,6 +13,25 @@ $fn2 = function ($x) use ($y) {
$date = new DateTimeImmutable ();
$date->format('Y-m-d');

DateTimeImmutable::createFromMutable(new \DateTime());
DateTimeImmutable::createFromMutable(new \DateTime('now'));

str_contains (\strtoupper(substr('abcdef', -2), 'f'));

/**
* Function declaration
*/
function testMe(string|int $name): int
{
if (empty($name)) {
return 0;
} elseif ($name === 1) {
return (int) $name;
}

switch($name) {
case '2':
return 2;
default:
throw new \Exception('error');
}
}

0 comments on commit 5bd46df

Please sign in to comment.