Skip to content

Commit

Permalink
Added scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Nov 5, 2016
1 parent 28aa801 commit dbf6ec9
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 5 deletions.
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Quality assurance for your PHP projects
composer require --dev ninjify/qa
```

## Usage
## Manual usage (bin)

### CodeSniffer

Expand Down Expand Up @@ -43,3 +43,58 @@ Default folders are: `src`, `app`, `tests`
vendor/bin/linter
vendor/bin/linter <folder1> <folder2>
```

## Composer scripts

### Extra scripts [composer.json]
You should define a special scripts to your `composer.json`.

```json
"scripts": {
"qa": [
"@qa-codesniffer",
"@qa-linter"
],
"qa-codesniffer": [
"Ninjify\\Composer\\Script\\CodeSniffer::execute"
],
"qa-codefixer": [
"Ninjify\\Composer\\Script\\CodeFixer::execute"
],
"qa-linter": [
"Ninjify\\Composer\\Script\\Linter::execute"
]
}
```

### Extra parameters [composer.json]

```json
"extra": {
"ninjify": {
"qa": {
"codesniffer": {
"folders": ["app", "src", "test/cases"]
},
"codefixer": {
"folders": ["app", "src", "test/cases"]
},
"linter": {
"folders": ["app", "src", "test/cases"]
}
}
}
}
```

### Executing

```
composer qa
composer run qa
composer run-script qa
```

-----

Thanks for testing, reporting and contributing.
4 changes: 2 additions & 2 deletions bin/codefixer
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ if [ -z "$1" ]; then
[ -d "app" ] && FOLDERS+=('app')
[ -d "src" ] && FOLDERS+=('src')
[ -d "tests" ] && FOLDERS+=('tests')
${DIR}/phpcbf --standard=${RULESET} --extensions=${EXTENSIONS} --colors --encoding=utf-8 --no-patch -nsp ${FOLDERS[*]}
${DIR}/phpcbf --standard="${RULESET}" --extensions="${EXTENSIONS}" --colors --encoding=utf-8 --no-patch -nsp ${FOLDERS[*]}
else
# Run codefixer
${DIR}/phpcbf --standard=${RULESET} --extensions=${EXTENSIONS} --colors --encoding=utf-8 --no-patch -nsp $@
${DIR}/phpcbf --standard="${RULESET}" --extensions="${EXTENSIONS}" --colors --encoding=utf-8 --no-patch -nsp $@
fi
4 changes: 2 additions & 2 deletions bin/codesniffer
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ if [ -z "$1" ]; then
[ -d "app" ] && FOLDERS+=('app')
[ -d "src" ] && FOLDERS+=('src')
[ -d "tests" ] && FOLDERS+=('tests')
${DIR}/phpcs --standard=${RULESET} --extensions=${EXTENSIONS} --encoding=utf-8 --colors -nsp ${FOLDERS[*]}
${DIR}/phpcs --standard="${RULESET}" --extensions="${EXTENSIONS}" --encoding=utf-8 --colors -nsp ${FOLDERS[*]}
else
# Run codesniffer
${DIR}/phpcs --standard=${RULESET} --extensions=${EXTENSIONS} --encoding=utf-8 --colors -nsp $@
${DIR}/phpcs --standard="${RULESET}" --extensions="${EXTENSIONS}" --encoding=utf-8 --colors -nsp $@
fi
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
"jakub-onderka/php-console-highlighter": "^0.3.2",
"jakub-onderka/php-parallel-lint": "^0.9.2"
},
"require-dev": {
"composer/composer": "^1.2.2"
},
"autoload": {
"psr-4": {
"Ninjify\\": "src"
}
},
"bin": [
"bin/codesniffer",
"bin/codefixer",
Expand Down
8 changes: 8 additions & 0 deletions src/Composer/Script/AbstractScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Ninjify\Composer\Script;

abstract class AbstractScript
{

}
84 changes: 84 additions & 0 deletions src/Composer/Script/CodeFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Ninjify\Composer\Script;

use Composer\Script\Event;

final class CodeFixer extends AbstractScript
{

/** @var Event */
private $event;

/** @var array */
private $config;

/**
* @param Event $event
* @param array $config
*/
public function __construct(Event $event, array $config)
{
$this->event = $event;
$this->config = $config;
}

/**
* API *********************************************************************
* *************************************************************************
*/

/**
* @param Event $event
* @return void
*/
public static function execute(Event $event)
{
$extra = $event->getComposer()->getPackage()->getExtra();

if (!isset($extra['ninjify'])) {
$event->getIO()->writeError('You have to setup "ninjify" in "extra" section.');

return;
}

if (!isset($extra['ninjify']['qa'])) {
$event->getIO()->writeError('You have to setup "qa" in "extra.ninjify" section.');

return;
}

if (!isset($extra['ninjify']['qa']['codefixer'])) {
$event->getIO()->writeError('You have to setup "codefixer" in "extra.ninjify.qa" section.');

return;
}

// CodeFixer config
$config = $extra['ninjify']['qa']['codefixer'];
$script = new CodeFixer($event, $config);
$script->process();
}

/**
* IMPLEMENTATION **********************************************************
* *************************************************************************
*/

/**
* @return void
*/
public function process()
{
if (!isset($this->config['folders']) || empty($this->config['folders'])) {
$this->event->getIO()->writeError('You have to add any folders in "extra.ninjify.qa.codefixer.folders" section.');

return;
}

$folders = $this->config['folders'];
$binDir = $this->event->getComposer()->getConfig()->get('bin-dir');
passthru(sprintf('%s/codefixer %s', $binDir, implode(' ', $folders)));
}

}
84 changes: 84 additions & 0 deletions src/Composer/Script/CodeSniffer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Ninjify\Composer\Script;

use Composer\Script\Event;

final class CodeSniffer extends AbstractScript
{

/** @var Event */
private $event;

/** @var array */
private $config;

/**
* @param Event $event
* @param array $config
*/
public function __construct(Event $event, array $config)
{
$this->event = $event;
$this->config = $config;
}

/**
* API *********************************************************************
* *************************************************************************
*/

/**
* @param Event $event
* @return void
*/
public static function execute(Event $event)
{
$extra = $event->getComposer()->getPackage()->getExtra();

if (!isset($extra['ninjify'])) {
$event->getIO()->writeError('You have to setup "ninjify" in "extra" section.');

return;
}

if (!isset($extra['ninjify']['qa'])) {
$event->getIO()->writeError('You have to setup "qa" in "extra.ninjify" section.');

return;
}

if (!isset($extra['ninjify']['qa']['codesniffer'])) {
$event->getIO()->writeError('You have to setup "codesniffer" in "extra.ninjify.qa" section.');

return;
}

// CodeSniffer config
$config = $extra['ninjify']['qa']['codesniffer'];
$script = new CodeSniffer($event, $config);
$script->process();
}

/**
* IMPLEMENTATION **********************************************************
* *************************************************************************
*/

/**
* @return void
*/
public function process()
{
if (!isset($this->config['folders']) || empty($this->config['folders'])) {
$this->event->getIO()->writeError('You have to add any folders in "extra.ninjify.qa.codesniffer.folders" section.');

return;
}

$folders = $this->config['folders'];
$binDir = $this->event->getComposer()->getConfig()->get('bin-dir');
passthru(sprintf('%s/codesniffer %s', $binDir, implode(' ', $folders)));
}

}
84 changes: 84 additions & 0 deletions src/Composer/Script/Linter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Ninjify\Composer\Script;

use Composer\Script\Event;

final class Linter extends AbstractScript
{

/** @var Event */
private $event;

/** @var array */
private $config;

/**
* @param Event $event
* @param array $config
*/
public function __construct(Event $event, array $config)
{
$this->event = $event;
$this->config = $config;
}

/**
* API *********************************************************************
* *************************************************************************
*/

/**
* @param Event $event
* @return void
*/
public static function execute(Event $event)
{
$extra = $event->getComposer()->getPackage()->getExtra();

if (!isset($extra['ninjify'])) {
$event->getIO()->writeError('You have to setup "ninjify" in "extra" section.');

return;
}

if (!isset($extra['ninjify']['qa'])) {
$event->getIO()->writeError('You have to setup "qa" in "extra.ninjify" section.');

return;
}

if (!isset($extra['ninjify']['qa']['linter'])) {
$event->getIO()->writeError('You have to setup "linter" in "extra.ninjify.qa" section.');

return;
}

// Linter config
$config = $extra['ninjify']['qa']['linter'];
$script = new Linter($event, $config);
$script->process();
}

/**
* IMPLEMENTATION **********************************************************
* *************************************************************************
*/

/**
* @return void
*/
public function process()
{
if (!isset($this->config['folders']) || empty($this->config['folders'])) {
$this->event->getIO()->writeError('You have to add any folders in "extra.ninjify.qa.linter.folders" section.');

return;
}

$folders = $this->config['folders'];
$binDir = $this->event->getComposer()->getConfig()->get('bin-dir');
passthru(sprintf('%s/linter %s', $binDir, implode(' ', $folders)));
}

}

0 comments on commit dbf6ec9

Please sign in to comment.