Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
William Salemé committed Jul 5, 2017
1 parent f26267e commit 214fd1e
Show file tree
Hide file tree
Showing 14 changed files with 1,041 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.idea/
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: php
php:
- 5.6
install: composer install --prefer-source
script:
- make sniff
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# phpcs 2.0+ Laravel 4/5 Command
[![Build Status](https://travis-ci.org/ChefsPlate/sniffer-rules.svg?branch=master)](https://travis-ci.org/ChefsPlate/sniffer-rules)
[![Latest Stable Version](https://poser.pugx.org/ChefsPlate/sniffer-rules/version.png)](https://packagist.org/packages/ChefsPlate/sniffer-rules)
[![License](https://poser.pugx.org/ChefsPlate/sniffer-rules/license.svg)](https://packagist.org/packages/ChefsPlate/sniffer-rules)

This is a [Laravel](http://laravel.com/) 4/5 package that hooks up
[SquizLabs CodeSniffer 2](https://github.com/squizlabs/PHP_CodeSniffer)
into Laravel-based apps. It can also be used manually, so read on.

Detect violations of a defined coding standard. It helps your code remain
clean and consistent. Available options are: **PSR2**, **PSR1**, **Zend**,
**PEAR**, **Squiz**, **PHPCS** and **ChefsPlate**.

### Setup

Require this package in composer:

```
$ composer require chefsplate/sniffer-rules
```

#### Laravel 4/5

In your `config/app.php` add `ChefsPlate\SnifferRules\ServiceProvider:class`
to `$providers` array:

```php
'providers' => [
...

ChefsPlate\SnifferRules\ServiceProvider::class,

...
],
```
#### Laravel 5: Publish the configuration file

```bash
$ php artisan vendor:publish
```

#### Laravel 4: Manually create config

Copy [config](src/ChefsPlate/SnifferRules/config/config.php) to
`app/config/sniffer-rules.php`

Edit configuration file `config/sniffer-rules.php` to tweak the sniffer behavior.

#### Manual

Install our _Standard_ by configuring **PHP_CodeSniffer** to look for it.

```bash
$ php ./vendor/bin/phpcs --config-set installed_paths ./vendor/chefsplate/src/ChefsPlate/SnifferRules/Standard/
```

### Usage
#### Laravel
```bash
$ php artisan sniff
```

To run the sniffer in a CI environment, the `-n` option should be set to remove
interaction:

```
$ php artisan sniff -n
```

#### Manual

```bash
$ php ./vendor/bin/phpcs --standard=ChefsPlate path/to/code
```

It's encouraged to add a [`Makefile`](Makefile) to your project that makes it
trivial for other developers. Use `Makefile` in this directory and adjust as
needed to fit your project requirements.

## ChefsPlate Coding Standards

### Coding standards

* [PSR 2 Coding Style Guide](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)
* [PSR 1 Coding Standards](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md)
* [PSR 0 Coding Standards](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md)

#### Addendum and Clarifications

* `namespace` should be on the same line as opening php tag. e.g.: `<?php namespace ChefsPlate\Amazing`
* Property names should be snake_case
* Test names should use underscores, not camelCase. e.g.: `test_cats_love_catnip`

## License

MIT
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "chefsplate/sniffer-rules",
"description": "A Laravel 4 and Laravel 5 SquizLabs Code Sniffer 2.0 artisan command. Detect violations of a defined coding standard. It helps your code remains clean and consistent.",
"keywords": ["laravel", "laravel 4", "laravel 5", "lumen", "code sniffer", "squizlabs", "phpcs", "artisan", "cli"],
"homepage": "https://github.com/chefsplate/sniffer-rules",
"license": "MIT",
"authors": [
{
"name": "ChefsPlate Development Team",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "~4.0|~5.0",
"illuminate/console": "~4.0|~5.0",
"squizlabs/php_codesniffer": "2.*"
},
"autoload": {
"psr-0": {
"ChefsPlate\\SnifferRules\\": "src/"
}
},
"minimum-stability": "stable"
}
207 changes: 207 additions & 0 deletions src/ChefsPlate/SnifferRules/Command/SniffCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php namespace ChefsPlate\SnifferRules\Command;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;

class SniffCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'sniff';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Detect violations of coding standard.';

/**
* The exit code of the command
* @var integer
*/
protected $exitCode = 0;

/**
* The Laravel application instance.
*
* @var \Illuminate\Foundation\Application
*/
public $app;

/**
* The Laravel Config component
*
* @var \Illuminate\Config\Repository
*/
public $config;

protected $binPath = './vendor/bin';

/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$this->app = $this->getLaravel();
$this->config = $this->app->make('config');

$options = $this->processOptions();
$options['extensions'] = 'php';
$options['colors'] = $this->getOutput()->isDecorated();

$command = $this->buildCommand($this->binPath . '/phpcs', $options);

$this->info('Running PHP Code Sniffer...');
$this->info($command);

passthru($command, $exitCode);

$this->info('Done.');

if (!$this->option('no-interaction') && $exitCode !== 0) {
$answer = $this->ask('Try to automatically fix issues? [Yn]', 'y');

if (strtolower($answer) == 'n') {
$this->info('Declined fixes.');

return $exitCode;
}

// Code beautifier takes all the same options (except for colors).
unset($options['colors']);

$command = $this->buildCommand($this->binPath . '/phpcbf', $options);

$this->info('Running PHP Code Beautifier...');
$this->info($command);

passthru($command, $exitCode);

$this->info('Done.');

}

return $exitCode;
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [];
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['report', null, InputOption::VALUE_OPTIONAL, 'Report type, see phpmd -h for info', null],
['report-file', null, InputOption::VALUE_OPTIONAL, 'File to write report to', null],
];
}

private function processOptions()
{
$standards = $this->config->get('sniffer-rules.standard', ['PSR2']);
$files = $this->config->get('sniffer-rules.files', ['app']);
$ignore = $this->config->get('sniffer-rules.ignored', []);

$seStandardKey = array_search('ChefsPlate', $standards);
if ($seStandardKey !== false) {
$standards[$seStandardKey] = dirname(dirname(__FILE__)) . '/Standard/ChefsPlate';
}

$ignoreNamespace = $this->config->get('sniffer-rules.ignoreNamespace', []);
$allowSnakeCaseMethodName = $this->config->get('sniffer-rules.allowSnakeCaseMethodName', []);

$options = [
'standards' => $standards,
'files' => $files,
'ignore' => $ignore,
'runtime-set' => [
'ignoreNamespace' => $ignoreNamespace,
'allowSnakeCaseMethodName' => $allowSnakeCaseMethodName
]
];

$options = array_merge($options, $this->getCliOptions());

return $options;
}

private function buildCommand($command, array $options)
{

$commandParts = [
'php',
$command,
];

// Standards requires special processing
foreach ($options['standards'] as $standardPath) {
$commandParts[] = sprintf('--standard="%s"', $standardPath);
}
unset($options['standards']);

// So does files...
$files = $options['files'];
unset($options['files']);

// And runtime-set..
foreach ($options['runtime-set'] as $configKey => $value) {
$commandParts[] = sprintf("--runtime-set '%s' '%s'", $configKey, json_encode($value));
}
unset($options['runtime-set']);

if (isset($options['colors']) && $options['colors'] === false) {
// don't pass --colors= if its false
unset($options['colors']);
}


foreach ($options as $name => $value) {
if ($value === true) {
$commandParts[] = '--' . $name;
} elseif (is_array($value)) {
$commandParts[] = sprintf('--%s="%s"', $name, implode(',', $value));
} else {
$commandParts[] = sprintf('--%s="%s"', $name, $value);
}
}

$commandParts = array_merge($commandParts, $files);
$command = implode(' ', $commandParts);

return $command;
}

/**
* Generates options array from passed in cli options
*
* @return array
*/
private function getCliOptions()
{
$validOptions = [];

foreach ($this->getOptions() as $option) {
$key = $option[0];
$validOptions[$key] = $this->option($key);
}

return array_filter($validOptions);
}
}
Loading

0 comments on commit 214fd1e

Please sign in to comment.