@@ -110,4 +110,170 @@ final class TestClass extends BaseClass implements \Iterator, Bar
110110
111111 $ this ->assertSame ($ expected , $ this ->printer ->prettyPrintFile ($ nodeTraverser ->traverse ($ this ->parser ->parse ('' ))));
112112 }
113+
114+ /**
115+ * @test
116+ */
117+ public function it_supports_sort_of_class_constants (): void
118+ {
119+ $ code = <<<'EOF'
120+ <?php
121+
122+ declare (strict_types=1);
123+ namespace My\Awesome\Service;
124+
125+ final class TestClass
126+ {
127+ protected const PROT = 'protected';
128+ private const PRIV = 'private';
129+ public const PUB = 'public';
130+ const FIRST = 1;
131+ }
132+ EOF;
133+
134+ $ ast = $ this ->parser ->parse ($ code );
135+
136+ $ classFactory = ClassBuilder::fromNodes (...$ ast );
137+
138+ $ classFactory ->sortConstants (function (ClassConstBuilder $ a , ClassConstBuilder $ b ) {
139+ return $ a ->getName () <=> $ b ->getName ();
140+ });
141+
142+ $ constants = $ classFactory ->getConstants ();
143+ $ this ->assertCount (4 , $ constants );
144+ $ this ->assertSame ('FIRST ' , $ constants [0 ]->getName ());
145+ $ this ->assertSame ('PRIV ' , $ constants [1 ]->getName ());
146+ $ this ->assertSame ('PROT ' , $ constants [2 ]->getName ());
147+ $ this ->assertSame ('PUB ' , $ constants [3 ]->getName ());
148+
149+ $ expected = <<<'EOF'
150+ <?php
151+
152+ declare (strict_types=1);
153+ namespace My\Awesome\Service;
154+
155+ final class TestClass
156+ {
157+ const FIRST = 1;
158+ private const PRIV = 'private';
159+ protected const PROT = 'protected';
160+ public const PUB = 'public';
161+ }
162+ EOF;
163+ $ nodeTraverser = new NodeTraverser ();
164+ $ classFactory ->injectVisitors ($ nodeTraverser , $ this ->parser );
165+
166+ $ this ->assertSame ($ expected , $ this ->printer ->prettyPrintFile ($ nodeTraverser ->traverse ($ this ->parser ->parse ('' ))));
167+ }
168+
169+ /**
170+ * @test
171+ */
172+ public function it_supports_sort_of_namespace_use (): void
173+ {
174+ $ code = <<<'EOF'
175+ <?php
176+
177+ declare (strict_types=1);
178+ namespace My\Awesome\Service;
179+
180+ use My\D;
181+ use My\A;
182+ use My\C;
183+ use My\B;
184+
185+ final class TestClass
186+ {
187+ }
188+ EOF;
189+
190+ $ ast = $ this ->parser ->parse ($ code );
191+
192+ $ classFactory = ClassBuilder::fromNodes (...$ ast );
193+
194+ $ classFactory ->sortNamespaceUse (function (string $ a , string $ b ) {
195+ return $ a <=> $ b ;
196+ });
197+
198+ $ namespaceUse = $ classFactory ->getNamespaceUse ();
199+ $ this ->assertCount (4 , $ namespaceUse );
200+ $ this ->assertSame ('My \\A ' , $ namespaceUse [0 ]);
201+ $ this ->assertSame ('My \\B ' , $ namespaceUse [1 ]);
202+ $ this ->assertSame ('My \\C ' , $ namespaceUse [2 ]);
203+ $ this ->assertSame ('My \\D ' , $ namespaceUse [3 ]);
204+
205+ $ expected = <<<'EOF'
206+ <?php
207+
208+ declare (strict_types=1);
209+ namespace My\Awesome\Service;
210+
211+ use My\A;
212+ use My\B;
213+ use My\C;
214+ use My\D;
215+ final class TestClass
216+ {
217+ }
218+ EOF;
219+ $ nodeTraverser = new NodeTraverser ();
220+ $ classFactory ->injectVisitors ($ nodeTraverser , $ this ->parser );
221+
222+ $ this ->assertSame ($ expected , $ this ->printer ->prettyPrintFile ($ nodeTraverser ->traverse ($ this ->parser ->parse ('' ))));
223+ }
224+
225+ /**
226+ * @test
227+ */
228+ public function it_supports_sort_of_traits (): void
229+ {
230+ $ code = <<<'EOF'
231+ <?php
232+
233+ declare (strict_types=1);
234+ namespace My\Awesome\Service;
235+
236+ final class TestClass
237+ {
238+ use My\D;
239+ use My\A;
240+ use My\C;
241+ use My\B;
242+ }
243+ EOF;
244+
245+ $ ast = $ this ->parser ->parse ($ code );
246+
247+ $ classFactory = ClassBuilder::fromNodes (...$ ast );
248+
249+ $ classFactory ->sortTraits (function (string $ a , string $ b ) {
250+ return $ a <=> $ b ;
251+ });
252+
253+ $ useTrait = $ classFactory ->getUseTrait ();
254+ $ this ->assertCount (4 , $ useTrait );
255+ $ this ->assertSame ('My \\A ' , $ useTrait [0 ]);
256+ $ this ->assertSame ('My \\B ' , $ useTrait [1 ]);
257+ $ this ->assertSame ('My \\C ' , $ useTrait [2 ]);
258+ $ this ->assertSame ('My \\D ' , $ useTrait [3 ]);
259+
260+ $ expected = <<<'EOF'
261+ <?php
262+
263+ declare (strict_types=1);
264+ namespace My\Awesome\Service;
265+
266+ final class TestClass
267+ {
268+ use My\A;
269+ use My\B;
270+ use My\C;
271+ use My\D;
272+ }
273+ EOF;
274+ $ nodeTraverser = new NodeTraverser ();
275+ $ classFactory ->injectVisitors ($ nodeTraverser , $ this ->parser );
276+
277+ $ this ->assertSame ($ expected , $ this ->printer ->prettyPrintFile ($ nodeTraverser ->traverse ($ this ->parser ->parse ('' ))));
278+ }
113279}
0 commit comments