Skip to content

Commit 34f66d5

Browse files
committed
Test indentation of the PHP 8.5 pipe operator and clone-with
PHP 8.5 adds two constructs that reach the indentation engine: $slug = $title |> trim(...) |> strtolower(...); return clone($this, [ 'alpha' => $alpha, ]); The pipe operator starts a continuation line with an operator, and "clone with" gives `clone' -- until now a bare prefix keyword -- an argument list. Both already indent correctly: `|>' matches php-indent--indent-operator-re, so a line starting with it is a statement continuation, and `clone(...)' is just a call as far as the bracket handling is concerned. Pin that down before it regresses. The fixtures cover the pipe at the top level, on the same line as the assignment, inside a function body, inside an argument list, and piping into static and instance methods; and clone-with inline, with a broken argument list, and next to a plain `clone $obj'. Verified the tests have teeth: flattening every line's leading whitespace and reindenting restores all 35 checked lines. See https://www.php.net/releases/8.5/en.php
1 parent f6cb997 commit 34f66d5

3 files changed

Lines changed: 91 additions & 0 deletions

File tree

tests/8.5/clone-with.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* PHP 8.5: `clone' with a second argument ("clone with").
5+
*
6+
* @see https://www.php.net/releases/8.5/en.php
7+
*/
8+
9+
// The example from the release announcement.
10+
readonly class Color
11+
{
12+
public function __construct( // ###php-mode-test### ((indent 4))
13+
public int $red, // ###php-mode-test### ((indent 8))
14+
public int $green, // ###php-mode-test### ((indent 8))
15+
public int $blue, // ###php-mode-test### ((indent 8))
16+
public int $alpha = 255, // ###php-mode-test### ((indent 8))
17+
) {} // ###php-mode-test### ((indent 4))
18+
19+
public function withAlpha(int $alpha): self // ###php-mode-test### ((indent 4))
20+
{ // ###php-mode-test### ((indent 4))
21+
return clone($this, [ // ###php-mode-test### ((indent 8))
22+
'alpha' => $alpha, // ###php-mode-test### ((indent 12))
23+
]); // ###php-mode-test### ((indent 8))
24+
} // ###php-mode-test### ((indent 4))
25+
}
26+
27+
$blue = new Color(79, 91, 147);
28+
$transparentBlue = $blue->withAlpha(128);
29+
30+
// All on one line.
31+
$copy = clone($blue, ['alpha' => 0]); // ###php-mode-test### ((indent 0))
32+
33+
// Plain `clone' keeps working.
34+
$plain = clone $blue; // ###php-mode-test### ((indent 0))
35+
36+
// The argument list broken across lines.
37+
$other = clone(
38+
$blue, // ###php-mode-test### ((indent 4))
39+
[ // ###php-mode-test### ((indent 4))
40+
'red' => 0, // ###php-mode-test### ((indent 8))
41+
'green' => 0, // ###php-mode-test### ((indent 8))
42+
], // ###php-mode-test### ((indent 4))
43+
); // ###php-mode-test### ((indent 0))

tests/8.5/pipe-operator.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* PHP 8.5: the pipe operator `|>'.
5+
*
6+
* @see https://www.php.net/releases/8.5/en.php
7+
*/
8+
9+
// The example from the release announcement.
10+
$title = ' PHP 8.5 Released ';
11+
12+
$slug = $title
13+
|> trim(...) // ###php-mode-test### ((indent 4))
14+
|> (fn($str) => str_replace(' ', '-', $str)) // ###php-mode-test### ((indent 4))
15+
|> (fn($str) => str_replace('.', '', $str)) // ###php-mode-test### ((indent 4))
16+
|> strtolower(...); // ###php-mode-test### ((indent 4))
17+
18+
// A pipe starting on the same line as the assignment.
19+
$result = $input |> trim(...) // ###php-mode-test### ((indent 0))
20+
|> strtolower(...); // ###php-mode-test### ((indent 4))
21+
22+
// Inside a function body.
23+
function slugify(string $title): string
24+
{
25+
return $title // ###php-mode-test### ((indent 4))
26+
|> trim(...) // ###php-mode-test### ((indent 8))
27+
|> strtolower(...); // ###php-mode-test### ((indent 8))
28+
}
29+
30+
// Inside an argument list.
31+
var_dump(
32+
$title // ###php-mode-test### ((indent 4))
33+
|> trim(...) // ###php-mode-test### ((indent 8))
34+
|> strtolower(...), // ###php-mode-test### ((indent 8))
35+
); // ###php-mode-test### ((indent 0))
36+
37+
// Piping into a static method and a method call.
38+
$out = $data
39+
|> Formatter::normalize(...) // ###php-mode-test### ((indent 4))
40+
|> $encoder->encode(...); // ###php-mode-test### ((indent 4))

tests/php-mode-test.el

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,14 @@ The fixture deliberately contains a syntactically invalid declaration
819819
"Test highlighting language constructs added in PHP 8.4."
820820
(with-php-mode-test ("8.4/property-hooks.php" :faces t)))
821821

822+
(ert-deftest php-mode-test-php85 ()
823+
"Test indentation of language constructs added in PHP 8.5.
824+
825+
The pipe operator `|>' introduces a continuation line that starts with
826+
an operator, and \"clone with\" gives `clone' an argument list."
827+
(with-php-mode-test ("8.5/pipe-operator.php" :indent t :magic t))
828+
(with-php-mode-test ("8.5/clone-with.php" :indent t :magic t)))
829+
822830
(ert-deftest php-mode-test-lang ()
823831
"Test highlighting for language constructs."
824832
(with-php-mode-test ("lang/class/anonymous-class.php" :indent t :magic t :faces t))

0 commit comments

Comments
 (0)