-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8e5ee37
Showing
10 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Daniel Król | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# mleko/wingman | ||
|
||
Sort composer.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "mleko/wingman", | ||
"description": "Sort composer.json", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Daniel Król", | ||
"email": "daniel@krol.me" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.0", | ||
"composer/composer": "^1.5" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^6.3" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Mleko\\Wingman\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Mleko\\Wingman\\": "tests/" | ||
} | ||
}, | ||
"scripts": { | ||
"test": "phpunit", | ||
"post-update-cmd": [ | ||
"Mleko\\Wingman\\Composer::format" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
forceCoversAnnotation="true" | ||
beStrictAboutCoversAnnotation="true" | ||
beStrictAboutOutputDuringTests="true" | ||
beStrictAboutTodoAnnotatedTests="true" | ||
verbose="true"> | ||
<testsuite> | ||
<directory suffix="Test.php">tests</directory> | ||
</testsuite> | ||
|
||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
|
||
namespace Mleko\Wingman; | ||
|
||
|
||
class Comparator | ||
{ | ||
private $keys = []; | ||
|
||
/** | ||
* @param string[] $keys | ||
*/ | ||
public function __construct(array $keys) | ||
{ | ||
$this->keys = $keys; | ||
} | ||
|
||
public function compare($keyA, $keyB): int | ||
{ | ||
$indexA = array_search($keyA, $this->keys); | ||
$indexB = array_search($keyB, $this->keys); | ||
// both found | ||
if (false !== $indexA && false !== $indexB) { | ||
return $indexA - $indexB; | ||
} | ||
// none found | ||
if ($indexA === $indexB) { | ||
return strcmp($keyA, $keyB); | ||
} | ||
return false === $indexA ? 1 : -1; | ||
} | ||
|
||
public function __invoke($keyA, $keyB) | ||
{ | ||
return $this->compare($keyA, $keyB); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
|
||
namespace Mleko\Wingman; | ||
|
||
|
||
use Composer\Script\Event; | ||
|
||
class Composer | ||
{ | ||
public static function format(Event $event): void | ||
{ | ||
$composerJsonPath = $event->getComposer()->getConfig()->getConfigSource()->getName(); | ||
if (!is_readable($composerJsonPath) || !is_writable($composerJsonPath)) { | ||
$event->getIO()->writeError("composer.json is not readable/writable"); | ||
exit(1); | ||
} | ||
$event->getIO()->write(sprintf("Formatting file: %s", $composerJsonPath)); | ||
$composerJson = json_decode(file_get_contents($composerJsonPath)); | ||
$fifer = new Wingman(); | ||
$composerJson = $fifer->format($composerJson); | ||
file_put_contents($composerJsonPath, json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
|
||
namespace Mleko\Wingman; | ||
|
||
|
||
class Wingman | ||
{ | ||
private static $order = [ | ||
"name", | ||
"description", | ||
"version", | ||
"type", | ||
"keywords", | ||
"homepage", | ||
"time", | ||
"license", | ||
"authors", | ||
"support", | ||
"require", | ||
"require-dev", | ||
"conflict", | ||
"replace", | ||
"provide", | ||
"suggest", | ||
"autoload", | ||
"autoload-dev", | ||
"include-path", | ||
"target-dir", | ||
"minimum-stability", | ||
"prefer-stable", | ||
"repositories", | ||
"config", | ||
"scripts", | ||
"extra", | ||
"bin", | ||
"archive", | ||
"non-feature-branches" | ||
]; | ||
|
||
private $keys = []; | ||
|
||
/** | ||
* Wingman constructor. | ||
* @param string[]|null $keys | ||
*/ | ||
public function __construct(?array $keys = null) | ||
{ | ||
$this->keys = null === $keys ? self::$order : $keys; | ||
} | ||
|
||
public function format($composerJson) | ||
{ | ||
return $this->sort($composerJson, new Comparator(self::$order)); | ||
} | ||
|
||
private function sort($item, callable $comparator) | ||
{ | ||
if (is_array($item)) { | ||
$copy = $item; | ||
uksort($copy, $comparator); | ||
return $copy; | ||
} | ||
if (is_object($item)) { | ||
$copy = (array)$item; | ||
uksort($copy, $comparator); | ||
return (object)$copy; | ||
} | ||
return $item; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Mleko\Wingman; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class ComparatorTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @param int $expected | ||
* @param string $keyA | ||
* @param string $keyB | ||
* @param string[] $keys | ||
* @dataProvider caseProvider | ||
*/ | ||
public function testCompare(int $expected, string $keyA, string $keyB, array $keys) | ||
{ | ||
$comparator = new Comparator($keys); | ||
$actual = $comparator->compare($keyA, $keyB); | ||
$this->assertEquals($expected, 0 <=> $actual); | ||
} | ||
|
||
public function caseProvider() | ||
{ | ||
return [ | ||
[0, "a", "a", []], | ||
[1, "a", "b", []], | ||
[-1, "a", "b", ["b"]], | ||
[-1, "a", "b", ["b", "a"]], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Mleko\Wingman; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class WingmanTest extends TestCase | ||
{ | ||
public function testFormat() | ||
{ | ||
$fifer = new Wingman(); | ||
$this->assertEquals( | ||
[ | ||
"name" => "mleko/wingman", | ||
"license" => "MIT" | ||
], | ||
$fifer->format([ | ||
"license" => "MIT", | ||
"name" => "mleko/wingman", | ||
]) | ||
); | ||
} | ||
} |