Skip to content
This repository was archived by the owner on Feb 21, 2022. It is now read-only.

Commit 30eeee2

Browse files
committed
Merge pull request #13 from hacfi/subgroups
Add subgroups to allow sorting within a group
2 parents ea95c2a + a036632 commit 30eeee2

File tree

3 files changed

+136
-10
lines changed

3 files changed

+136
-10
lines changed

README.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ formatter
141141

142142
### Sort all Use Statements
143143

144-
You can sort your Use Statements in a very different ways. For this command you
144+
You can sort your Use Statements in different ways. For this command you
145145
must provide as an argument the path where to look for the PHP files you want to
146146
process.
147147

@@ -164,12 +164,12 @@ $ php-formatter formatter:use:sort src/ --dry-run
164164

165165
#### Group
166166

167-
You can sort your Use statements using as many groups as yo want (***--group***).
167+
You can sort your Use statements using as many groups as you want (***--group***).
168168
It means that you can group lines with same root (***Symfony\\***) in a specific
169169
order.
170170

171171
Common group is named `_main` and if is not specified, is placed in the
172-
begining. You can define where to place this group with `--group` option
172+
beginning. You can define where to place this group with the `--group` option
173173

174174
``` bash
175175
$ php-formatter formatter:use:sort src/ --group="Mmoreram" --group="_main" --group="Symfony"
@@ -191,6 +191,18 @@ use Symfony\AnotherClass;
191191
As you can see, a blank line is placed between groups. If any group is defined,
192192
one big group is created with all namespaces.
193193

194+
When using a `.formatter.yml` you can also specify subgroups by entering an array
195+
196+
``` yml
197+
use-sort:
198+
group:
199+
- [Symfony\Component\HttpKernel, Symfony]
200+
- _main
201+
```
202+
203+
This will create a Symfony group placing all `Symfony\Component\HttpKernel` classes on top.
204+
205+
194206
Finally, the `--group-type` defines if you want one `use` literal in every
195207
namespace line
196208

@@ -210,7 +222,7 @@ use Mmoreram\MyClass;
210222
use Mmoreram\MySecondClass;
211223
```
212224

213-
or if you want only one use for all group.
225+
or if you want only one use for all groups.
214226

215227
``` bash
216228
$ php-formatter formatter:use:sort src/ --group="Mmoreram" --group-type="one"
@@ -302,7 +314,7 @@ use AnotherBundle\AnotherClass;
302314

303315
### Fix all PHP headers
304316

305-
You can define your PHP header in your `.formatter.yml` file an this command
317+
You can define your PHP header in your `.formatter.yml` file and this command
306318
will check and fix that all PHP files have it properly.
307319

308320
* command: `php-formatter formatter:header:fix`
@@ -311,7 +323,7 @@ will check and fix that all PHP files have it properly.
311323

312324
#### Dry run
313325

314-
You can use this tool just to test the files will be modified, using option
326+
You can use this tool just to test the files will be modified, using the option
315327
--dry-run
316328

317329
``` bash

src/PHPFormatter/Sorter/UseSorter.php

+31-3
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,16 @@ public function sort($data)
235235
continue;
236236
}
237237

238-
$groups[$groupKey] = $this->sortGroup($group);
238+
if (is_int($groupKey)) {
239+
$subGroupSorted = array();
240+
foreach ($group as $subGroupKey => $subGroup) {
241+
$subGroupSorted = array_merge($subGroupSorted, $this->sortGroup($subGroup));
242+
}
243+
244+
$groups[$groupKey] = $subGroupSorted;
245+
} else {
246+
$groups[$groupKey] = $this->sortGroup($group);
247+
}
239248
}
240249

241250
$doubleEOL = PHP_EOL . PHP_EOL;
@@ -258,7 +267,15 @@ function ($group) {
258267
*/
259268
private function createGroups(array $namespaces)
260269
{
261-
$groups = array_fill_keys($this->groups, array());
270+
$groups = array();
271+
272+
foreach ($this->groups as $group) {
273+
if (is_array($group)) {
274+
$groups[] = array_fill_keys($group, array());
275+
} else {
276+
$groups[$group] = array();
277+
}
278+
}
262279

263280
if (!array_key_exists('_main', $groups)) {
264281

@@ -272,8 +289,15 @@ private function createGroups(array $namespaces)
272289

273290
foreach ($groups as $groupKey => $group) {
274291

275-
if (strpos($namespace, $groupKey) === 0) {
292+
if (is_int($groupKey)) {
293+
foreach ($group as $subGroupKey => $subGroup) {
294+
if (strpos($namespace, $subGroupKey) === 0) {
295+
array_push($groups[$groupKey][$subGroupKey], $namespace);
276296

297+
continue 3;
298+
}
299+
}
300+
} elseif (is_string($groupKey) && strpos($namespace, $groupKey) === 0) {
277301
array_push($groups[$groupKey], $namespace);
278302

279303
continue 2;
@@ -295,6 +319,10 @@ private function createGroups(array $namespaces)
295319
*/
296320
private function sortGroup(array $group)
297321
{
322+
if (empty($group)) {
323+
return array();
324+
}
325+
298326
if ($this->sortType == self::SORT_TYPE_LENGTH) {
299327

300328
usort($group, function ($a, $b) {

tests/PHPFormatter/Sorter/UseSorterTest.php

+87-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,93 @@ public function dataSort()
294294
use Test3\\File;
295295
use Test3\\MyFolder\\Myclass;
296296
"
297-
)
297+
),
298+
array(
299+
['Test2', 'TestEmpty', '_main', 'Test3'],
300+
UseSorter::SORT_TYPE_ALPHABETIC,
301+
UseSorter::SORT_DIRECTION_ASC,
302+
UseSorter::GROUP_TYPE_EACH,
303+
"
304+
use Test2\\Myclass3;
305+
use Test2\\Myclass4;
306+
307+
308+
309+
use Test1\\Myclass1;
310+
use Test1\\Myclass2;
311+
use Test1\\MyFolder5\\File as MyFile;
312+
use Test4\\Myclass3;
313+
314+
use Test3\\File;
315+
use Test3\\MyFolder\\Myclass;
316+
"
317+
),
318+
array(
319+
['Test2', ['Test1\MyFolder5', 'Test1'], '_main'],
320+
UseSorter::SORT_TYPE_ALPHABETIC,
321+
UseSorter::SORT_DIRECTION_ASC,
322+
UseSorter::GROUP_TYPE_EACH,
323+
"
324+
use Test2\\Myclass3;
325+
use Test2\\Myclass4;
326+
327+
use Test1\\MyFolder5\\File as MyFile;
328+
use Test1\\Myclass1;
329+
use Test1\\Myclass2;
330+
331+
use Test3\\File;
332+
use Test3\\MyFolder\\Myclass;
333+
use Test4\\Myclass3;
334+
"
335+
),
336+
);
337+
}
338+
339+
/**
340+
* Test skip empty
341+
*/
342+
public function testGroupSkip()
343+
{
344+
$parsedData = $this
345+
->useSorter
346+
->setGroups(['Test2', 'TestEmpty', '_main', 'Test3'])
347+
->setSortType(UseSorter::SORT_TYPE_ALPHABETIC)
348+
->setSortDirection(UseSorter::SORT_DIRECTION_ASC)
349+
->setGroupType(UseSorter::GROUP_TYPE_EACH)
350+
->setGroupSkipEmpty(true)
351+
->sort($this->data);
352+
353+
$result =
354+
"
355+
use Test2\\Myclass3;
356+
use Test2\\Myclass4;
357+
358+
use Test1\\Myclass1;
359+
use Test1\\Myclass2;
360+
use Test1\\MyFolder5\\File as MyFile;
361+
use Test4\\Myclass3;
362+
363+
use Test3\\File;
364+
use Test3\\MyFolder\\Myclass;
365+
";
366+
$realResult =
367+
"<?php
368+
369+
/**
370+
* Copyright
371+
*/
372+
373+
namespace PHPFormatter\\Tests\\Mocks;
374+
$result
375+
/**
376+
* Class SimpleMock
377+
*/
378+
class SimpleMock
379+
{}";
380+
381+
$this->assertEquals(
382+
$realResult,
383+
$parsedData
298384
);
299385
}
300386
}

0 commit comments

Comments
 (0)