Skip to content

Commit

Permalink
Add Extended code sniffer to docker image.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiraud committed Sep 5, 2017
1 parent 2c98ab8 commit 64aadd5
Show file tree
Hide file tree
Showing 44 changed files with 3,299 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM php:7.1-fpm-alpine

MAINTAINER "Nicolas Giraud" <[email protected]>

COPY Standards/CodeSnifferExtended /data/Standards/CodeSnifferExtended

RUN rm -rf /data/Standards/CodeSnifferExtended/Docs \
&& curl -Ls https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar > /usr/local/bin/phpcs \
&& chmod +x /usr/local/bin/phpcs \
&& curl -Ls https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar > /usr/local/bin/phpcbf \
&& chmod +x /usr/local/bin/phpcbf \

&& phpcs --config-set installed_paths /data/Standards/CodeSnifferExtended \
&& phpcs --config-set default_standard CodeSnifferExtended \
&& phpcs --config-set encoding utf-8 \
&& rm -rf /var/cache/apk/* /var/tmp/* /tmp/*

VOLUME ["/data"]
WORKDIR /data/www

ENTRYPOINT ["phpcs"]
CMD ["--version"]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 nicodocker91
Copyright (c) 2017 nicodocker91 <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Tools: PHP Code Sniffer
=======================

| | master | develop |
|----------:|--------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Pipelines | [![build status](http://git.it.aareonit.fr/docker-dil/tools-phpcs/badges/master/build.svg)](http://git.it.aareonit.fr/docker-dil/tools-phpcs/commits/master) | [![build status](http://git.it.aareonit.fr/docker-dil/tools-phpcs/badges/develop/build.svg)](http://git.it.aareonit.fr/docker-dil/tools-phpcs/commits/develop) |


Contains the image of what Aareon is using to run PHP Code Sniffer tool on its projects.

## Usage

To launch the PHP Code Sniffer into the container, run:

docker run --rm --user $(id -u):$(id -g) --volume $(pwd):/data/www hub.aareonit.fr:5000/preprod/qa:phpcs <options> ${ROOT_FOLDER}

## Volumes

The only volume available to mount is `/data`.

The entrypoint is `/data/www`.

## Options

Run the following command to read all options available:

docker run --rm --user $(id -u):$(id -g) --volume $(pwd):/data/www hub.aareonit.fr:5000/preprod/qa:phpcs --help
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<documentation title="Space before and after concatenation operator.">
<standard>
<![CDATA[
This sniff check if you have a space before and after the concatenation operator.
]]>
</standard>
<code_comparison>
<code title="Valid: Correct spacing.">
<![CDATA[
$var = 'First string' . $string;
]]>
</code>
<code title="Invalid: no spaces before and after the concatenation operator.">
<![CDATA[
$var = 'First string'.$string;
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<documentation title="Too many parameters declared in function and methods.">
<standard>
<![CDATA[
This sniff prohibits the use of too many parameters declared in functions and methods.
]]>
</standard>
<code_comparison>
<code title="Valid: correct number of parameters.">
<![CDATA[
/**
* @return void
*/
public function testFunction($arg1, $arg2, $arg3)
{
//Methods have internal $this argument. So, maximum is only 3.
return;
}
/**
* @return void
*/
public static function testFunction2($arg1, $arg2, $arg3, $arg4)
{
//Static methods do not have $this argument. So, maximum is 4.
return;
}
/**
* @return void
*/
function function3($arg1, $arg2, $arg3, $arg4)
{
//Functions do not have $this argument. So, maximum is 4.
return;
}
]]>
</code>
<code title="Invalid: too many parameters for method or functions.">
<![CDATA[
/**
* @return void
*/
public function testFunction($arg1, $arg2, $arg3, $arg4)
{
//Methods have internal $this argument. So, maximum is only 3.
return;
}
/**
* @return void
*/
public static function testFunction2($arg1, $arg2, $arg3, $arg4, $arg5)
{
//Static methods do not have $this argument. So, maximum is 4.
return;
}
/**
* @return void
*/
function function3($arg1, $arg2, $arg3, $arg4, $arg5)
{
//Functions do not have $this argument. So, maximum is 4.
return;
}
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<documentation title="Elvis operator can be used.">
<standard>
<![CDATA[
This sniff warn the user that the elvis operator (?:) can be used.
]]>
</standard>
<code_comparison>
<code title="Valid: usage of elvis operator.">
<![CDATA[
$b = $a ?: $c;
]]>
</code>
<code title="Invalid: usage of ternary operator.">
<![CDATA[
$b = $a ? $a : $c;
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<documentation title="Nested not operator usage.">
<standard>
<![CDATA[
This sniff prohibits the boolean not operator more that once.
]]>
</standard>
<code_comparison>
<code title="Valid: cast instead a double not operator.">
<![CDATA[
$var = (bool)$a;
]]>
</code>
<code title="Invalid: nested operator.">
<![CDATA[
$var = !!$a;
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<documentation title="Nested positive ifs.">
<standard>
<![CDATA[
This sniff detects useless nested if statements.
]]>
</standard>
<code_comparison>
<code title="Valid: single if condition grouped.to avoid nesting levels.">
<![CDATA[
if (true === $a && true === $b) {
//... do something
}
]]>
</code>
<code title="Invalid: useless nested levels of condition.">
<![CDATA[
if (true === $a) {
if (true === $b) {
//... do something
}
}
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<documentation title="Prefix increment or decrement.">
<standard>
<![CDATA[
This sniff detects $a += 1 or $a -= 1 instead of increment or decrement operator usage.
]]>
</standard>
<code_comparison>
<code title="Valid: prefix/suffix incrementing/decrementing used.">
<![CDATA[
$a--;
$a++;
]]>
</code>
<code title="Invalid: prefix/suffix incrementing/decrementing not used.">
<![CDATA[
$a -= 1;
$a += 1;
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<documentation title="Traditional array syntax.">
<standard>
<![CDATA[
This sniff prohibits the use of traditional array syntax like array(...) instead of [...].
]]>
</standard>
<code_comparison>
<code title="Valid: use the short syntax.">
<![CDATA[
$a = [];
]]>
</code>
<code title="Invalid: use the elder syntax.">
<![CDATA[
$a = array();
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<documentation title="Deprecated old constructor style.">
<standard>
<![CDATA[
This sniff avoid usage of deprecated old style constructor.
]]>
</standard>
<code_comparison>
<code title="Valid: usage of __constructor magic method.">
<![CDATA[
class Toto
{
public function __construct()
{
//Good constructor usage.
}
}
]]>
</code>
<code title="Invalid: usage of old constructor.">
<![CDATA[
class Toto
{
public function Toto()
{
//Old constructor style deprecated since PHP 5.
}
}
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<documentation title="Use time() function instead of mktime() or gmmktime() if they have no arguments.">
<standard>
<![CDATA[
This sniff avoid usage of gmmktime() and mktime() functions if they have no arguments.
]]>
</standard>
<code_comparison>
<code title="Valid: usage of time() function.">
<![CDATA[
$time = time();
]]>
</code>
<code title="Invalid: usage of mktime() of gmmktime() functions.">
<![CDATA[
$time = mktime();
// or
$time = gmmktime();
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<documentation title="Use Mersenne Twister functions instead of classic functions from the random API.">
<standard>
<![CDATA[
This sniff avoid usage of default random API functions and prefer using the Mersenne Twister random API functions.
]]>
</standard>
<code_comparison>
<code title="Valid: usages of Mersenne Twister functions.">
<![CDATA[
mt_rand();
mt_srand();
mt_getrandmax();
]]>
</code>
<code title="Invalid: usages of classic random functions.">
<![CDATA[
rand();
srand();
getrandmax();
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<documentation title="Ternary operator could be simplified.">
<standard>
<![CDATA[
This sniff reports when useless definition of <code>true</code> and <code>false</code> are used in ternary operations.
]]>
</standard>
<code_comparison>
<code title="Valid: operation is sufficient by itself.">
<![CDATA[
(1 === true);
]]>
</code>
<code title="Invalid: useless usage ternary operation here.">
<![CDATA[
(1 === true ? true : false);
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<documentation title="Check the order integrity of catches and check there are no double definition of the same exception catching type.">
<standard>
<![CDATA[
This sniff detects if you catch several times the same exception, or you catch \Exception in first when you have several exception types catching later.
]]>
</standard>
<code_comparison>
<code title="Valid: orders of catching execution are good and no catching twice the same exception type.">
<![CDATA[
try {
// Do something...
} catch (OtherException $other) {
// Come here if OtherException is thrown.
} catch (\Exception $e) {
// Come here if any exception but not OtherException is thrown.
}
]]>
</code>
<code title="Invalid: orders of catching execution are not good as generic \Exception is caught before everything else. Also, try to catch twice the same exception type.">
<![CDATA[
//Catching order is not relevant.
try {
// Do something...
} catch (\Exception $e) {
// Come here if any exception is thrown.
} catch (OtherException $other) {
// You cannot reach this statement.
}
//Catching twice the same exception type.
try {
// Do something...
} catch (MySuperException $e) {
// Come here if MySuperException is thrown.
} catch (MySuperException $other) {
// You cannot reach this statement as already defined above.
}
]]>
</code>
</code_comparison>
</documentation>
Loading

0 comments on commit 64aadd5

Please sign in to comment.