Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mleko committed Aug 10, 2017
0 parents commit 8e5ee37
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/composer.lock
21 changes: 21 additions & 0 deletions LICENSE.md
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.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mleko/wingman

Sort composer.json
35 changes: 35 additions & 0 deletions composer.json
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"
]
}
}
19 changes: 19 additions & 0 deletions phpunit.xml.dist
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>
38 changes: 38 additions & 0 deletions src/Comparator.php
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);
}
}
24 changes: 24 additions & 0 deletions src/Composer.php
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");
}
}
71 changes: 71 additions & 0 deletions src/Wingman.php
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;
}
}
33 changes: 33 additions & 0 deletions tests/ComparatorTest.php
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"]],
];
}
}
23 changes: 23 additions & 0 deletions tests/WingmanTest.php
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",
])
);
}
}

0 comments on commit 8e5ee37

Please sign in to comment.