-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
975f5b0
commit 24980dc
Showing
10 changed files
with
302 additions
and
9 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
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
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,52 @@ | ||
<?php | ||
|
||
namespace SLLH\ComposerLint; | ||
|
||
/** | ||
* @author Sullivan Senechal <soullivaneuh@gmail.com> | ||
*/ | ||
final class Linter | ||
{ | ||
/** | ||
* @param array $manifest composer.json file manifest | ||
* | ||
* @return string[] | ||
*/ | ||
public function validate($manifest) | ||
{ | ||
$errors = array(); | ||
|
||
if (isset($manifest['config']['sort-packages']) && $manifest['config']['sort-packages']) { | ||
foreach (array('require', 'require-dev', 'conflict', 'replace', 'provide', 'suggest') as $linksSection) { | ||
if (array_key_exists($linksSection, $manifest) && !$this->packagesAreSorted($manifest[$linksSection])) { | ||
array_push($errors, 'Links under '.$linksSection.' section are not sorted.'); | ||
} | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
private function packagesAreSorted(array $packages) | ||
{ | ||
$names = array_keys($packages); | ||
|
||
$hasPHP = in_array('php', $names); | ||
$extNames = array_filter($names, function ($name) { | ||
return 'ext-' === substr($name, 0, 4) && !strstr($name, '/'); | ||
}); | ||
sort($extNames); | ||
$vendorName = array_filter($names, function ($name) { | ||
return 'ext-' !== substr($name, 0, 4) && 'php' !== $name; | ||
}); | ||
sort($vendorName); | ||
|
||
$sortedNames = array_merge( | ||
$hasPHP ? array('php') : array(), | ||
$extNames, | ||
$vendorName | ||
); | ||
|
||
return $sortedNames === $names; | ||
} | ||
} |
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
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,53 @@ | ||
<?php | ||
|
||
namespace SLLH\ComposerLint\Tests; | ||
|
||
use Composer\Json\JsonFile; | ||
use SLLH\ComposerLint\Linter; | ||
|
||
/** | ||
* @author Sullivan Senechal <soullivaneuh@gmail.com> | ||
*/ | ||
final class LinterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Linter | ||
*/ | ||
private $linter; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->linter = new Linter(); | ||
} | ||
|
||
/** | ||
* @dataProvider getLintData | ||
* | ||
* @param string $file | ||
* @param int $expectedErrorsCount | ||
*/ | ||
public function testLint($file, $expectedErrorsCount = 0) | ||
{ | ||
$json = new JsonFile($file); | ||
$manifest = $json->read(); | ||
|
||
$errors = $this->linter->validate($manifest); | ||
$this->assertCount($expectedErrorsCount, $errors); | ||
} | ||
|
||
/** | ||
* @return array[] | ||
*/ | ||
public function getLintData() | ||
{ | ||
return array( | ||
array(__DIR__.'/fixtures/sort-ok.json'), | ||
array(__DIR__.'/fixtures/sort-ko.json', 6), | ||
array(__DIR__.'/fixtures/sort-ko-deactivated.json'), | ||
array(__DIR__.'/fixtures/sort-ko-no-config.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,16 @@ | ||
{ | ||
"name": "foo/bar", | ||
"description": "Fake package. LULZ.", | ||
"license": "MIT", | ||
"require": { | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0", | ||
"aztech/layers": "0.1.*" | ||
}, | ||
"require-dev": { | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0", | ||
"aztech/layers": "0.1.*" | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
} | ||
} |
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,36 @@ | ||
{ | ||
"require": { | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0", | ||
"php": "^5.3" | ||
}, | ||
"require-dev": { | ||
"php": "^5.3", | ||
"aztech/layers": "0.1.*", | ||
"ext-aaa": "^1.0", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"conflict": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"replace": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"provide": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"suggest": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"config": { | ||
"sort-packages": false | ||
} | ||
} |
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 @@ | ||
{ | ||
"require": { | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0", | ||
"php": "^5.3" | ||
}, | ||
"require-dev": { | ||
"php": "^5.3", | ||
"aztech/layers": "0.1.*", | ||
"ext-aaa": "^1.0", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"conflict": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"replace": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"provide": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"suggest": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
} | ||
} |
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,36 @@ | ||
{ | ||
"require": { | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0", | ||
"php": "^5.3" | ||
}, | ||
"require-dev": { | ||
"php": "^5.3", | ||
"aztech/layers": "0.1.*", | ||
"ext-aaa": "^1.0", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"conflict": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"replace": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"provide": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"suggest": { | ||
"zzz/xxx": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
} | ||
} |
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,31 @@ | ||
{ | ||
"require": { | ||
"php": "^5.3", | ||
"ext-mbstring": "^1.0", | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"require-dev": { | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"conflict": { | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"replace": { | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"provide": { | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"suggest": { | ||
"aztech/layers": "0.1.*", | ||
"sllh/php-cs-fixer-styleci-bridge": "^2.0" | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
} | ||
} |