@@ -22,7 +22,7 @@ function number(int $number): int
2222number(-5);
2323PHP;
2424
25- $ transformed = StreamWrapper::transformSource ($ source , 'test4 .php ' );
25+ $ transformed = StreamWrapper::transformSource ($ source , 'test_params .php ' );
2626
2727 $ origLines = explode ("\n" , str_replace ("\r\n" , "\n" , $ source ));
2828 $ transLines = explode ("\n" , str_replace ("\r\n" , "\n" , $ transformed ));
@@ -56,7 +56,7 @@ public function __construct(public array $numbers)
5656new Numbers(['a', 'b', 'c', 1]);
5757PHP;
5858
59- $ transformed = StreamWrapper::transformSource ($ source , 'test8 .php ' );
59+ $ transformed = StreamWrapper::transformSource ($ source , 'test_cpm .php ' );
6060
6161 $ origLines = explode ("\n" , str_replace ("\r\n" , "\n" , $ source ));
6262 $ transLines = explode ("\n" , str_replace ("\r\n" , "\n" , $ transformed ));
@@ -68,4 +68,69 @@ public function __construct(public array $numbers)
6868
6969 expect ($ transCallLine )->toBe ($ origCallLine );
7070 });
71+
72+ test ('transforms single-line empty methods and constructors without shifting line numbers ' , function () {
73+ $ source = <<<'PHP'
74+ <?php
75+
76+ declare(strict_types=1);
77+
78+ class SingleLineBlocks
79+ {
80+ public function __construct() {}
81+
82+ public function emptyMethod(): void {}
83+
84+ /** @param string $val */
85+ public function doNothing(string $val) {}
86+ }
87+
88+ $obj = new SingleLineBlocks();
89+ PHP;
90+
91+ $ transformed = StreamWrapper::transformSource ($ source , 'test_single_line.php ' );
92+
93+ $ origLines = explode ("\n" , str_replace ("\r\n" , "\n" , $ source ));
94+ $ transLines = explode ("\n" , str_replace ("\r\n" , "\n" , $ transformed ));
95+
96+ expect (\count ($ transLines ))->toBe (\count ($ origLines ));
97+
98+ $ origCallLine = array_search ('$obj = new SingleLineBlocks(); ' , array_map ('trim ' , $ origLines ), true );
99+ $ transCallLine = array_search ('$obj = new SingleLineBlocks(); ' , array_map ('trim ' , $ transLines ), true );
100+
101+ expect ($ transCallLine )->toBe ($ origCallLine );
102+ });
103+
104+ test ('transforms generic single-line constructors perfectly (Edge Case Reproduction) ' , function () {
105+ $ source = <<<'PHP'
106+ <?php
107+
108+ namespace App;
109+
110+ /**
111+ * Covariant Producer Wrapper with single-line constructor
112+ *
113+ * @template-covariant T
114+ */
115+ class Producer
116+ {
117+ /** @param T $item */
118+ public function __construct(public mixed $item) {}
119+ }
120+
121+ $p = new Producer('test');
122+ PHP;
123+
124+ $ transformed = StreamWrapper::transformSource ($ source , 'test_producer.php ' );
125+
126+ $ origLines = explode ("\n" , str_replace ("\r\n" , "\n" , $ source ));
127+ $ transLines = explode ("\n" , str_replace ("\r\n" , "\n" , $ transformed ));
128+
129+ expect (\count ($ transLines ))->toBe (\count ($ origLines ));
130+
131+ $ origCallLine = array_search ("\$p = new Producer('test'); " , array_map ('trim ' , $ origLines ), true );
132+ $ transCallLine = array_search ("\$p = new Producer('test'); " , array_map ('trim ' , $ transLines ), true );
133+
134+ expect ($ transCallLine )->toBe ($ origCallLine );
135+ });
71136});
0 commit comments