Skip to content

Instantly share code, notes, and snippets.

Avatar
🤧
Code sniffing

Greg Sherwood gsherwood

🤧
Code sniffing
View GitHub Profile
@gsherwood
gsherwood / InlineControlStructureSniff.php
Created Mar 23, 2018
Generic/InlineControlStructure: fixer moves new PHPCS annotations #1932
View InlineControlStructureSniff.php
<?php
/**
* Verifies that inline control statements are not present.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\ControlStructures;
@gsherwood
gsherwood / JSTokenizer.php
Created Nov 21, 2016
Using PHP_CodeSniffer to tokenize a JS file
View JSTokenizer.php
<?php
namespace MyCustomProject;
use PHP_CodeSniffer\Runner;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\DummyFile;
// Include the PHPCS autoloader, however you need to.
require_once __DIR__.'/autoload.php';
@gsherwood
gsherwood / CustomPHPCSRunner.php
Created Sep 8, 2016
A custom runner for PHP_CodeSniffer to process a single piece of content
View CustomPHPCSRunner.php
<?php
namespace MyCustomProject;
use PHP_CodeSniffer\Runner;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Reporter;
use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Util\Timing;
// Include the PHPCS autoloader however you need.
View output
$ cat temp.php
<?php
class Client extends GuzzleClient implements ClientInterface {
/**
* {@inheritdoc}
*/
public function authenticatedRequest($method = 'GET', $url = null, UserInterface $user, $options = []) {
$duration = isset($options['timeout']) ? $options['timeout'] : NULL;
$options['query']['token'] = $user->acquireToken($duration);
@gsherwood
gsherwood / after.php
Created Jul 11, 2015
PHPCBF if/elseif/else test
View after.php
<?php
if ($foo) {
echo 'foo';
} elseif ($bar) {
echo 'bar';
} else {
echo 'else';
}
@gsherwood
gsherwood / SearchStringSniff.php
Created Jan 12, 2015
Example sniff to find a string with specific content
View SearchStringSniff.php
<?php
/**
* Generic_Sniffs_Strings_SearchStringSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
@gsherwood
gsherwood / temp.php
Created Dec 18, 2014
phpcbf temp.php --standard=PSR2 --tab-width=4
View temp.php
<?php
namespace I18nMessages\Test\I18n;
use Cake\TestSuite\TestCase;
use I18nMessages\I18n\DbMessagesLoader;
/**
* Tests for DbMessagesLoader
*/
class DbMessagesLoaderTest extends TestCase
@gsherwood
gsherwood / ruleset.xml
Created Dec 5, 2014
PSR2 with tabs instead of spaces
View ruleset.xml
<?xml version="1.0"?>
<ruleset name="MyStandard">
<description>PSR2 with tabs instead of spaces.</description>
<arg name="tab-width" value="4"/>
<rule ref="PSR2">
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
</rule>
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
@gsherwood
gsherwood / 1. temp.php (before fixing)
Created Nov 4, 2014
PHPCS/PHPCBF PSR-2 auto-formatting example
View 1. temp.php (before fixing)
<?php
namespace Vendor\Package;
use FooInterface; use BarClass as Bar; use OtherVendor\OtherPackage\BazClass;
class Foo extends Bar implements FooInterface {
public function sampleFunction($a,$b=null) {
if ($a === $b) { bar();
} else if ($a>$b) {
$foo->bar( $arg1 ); }
else {
BazClass::bar($arg2,$arg3 );
View mystandard.xml
<?xml version="1.0"?>
<ruleset name="PSR2">
<description>The PSR-2 coding standard.</description>
<!-- PHP code MUST use the long <?php ?> tags or the short-echo <?= ?> tags; it MUST NOT use the other tag variations. -->
<rule ref="Generic.PHP.DisallowShortOpenTag.EchoFound">
<severity>0</severity>
</rule>
<!-- PHP code MUST use only UTF-8 without BOM. -->
You can’t perform that action at this time.