Skip to content

Commit

Permalink
Merge pull request SnapInteractive#1 from rhowardiv/topsr2
Browse files Browse the repository at this point in the history
PSR2 for snap
  • Loading branch information
rhowardiv committed Jan 2, 2013
2 parents 55619f5 + 83a499b commit 7e04d48
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions CodeSniffer.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
$phpCodeSnifferConfig = array (
'default_standard' => 'Snap',
)
?>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
class PEAR_Sniffs_Functions_FunctionDeclarationSniff implements PHP_CodeSniffer_Sniff
{

/**
* Whitespace characters req'd per indent level
* @var int
*/
public $indent = 4;

/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -236,7 +241,7 @@ public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $st
}//end if
}//end if

// Each line between the parenthesis should be indented 4 spaces.
// Each line between the parenthesis should be indented.
$openBracket = $tokens[$stackPtr]['parenthesis_opener'];
$lastLine = $tokens[$openBracket]['line'];
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
Expand All @@ -249,7 +254,7 @@ public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $st
// as the function.
$expectedIndent = $functionIndent;
} else {
$expectedIndent = ($functionIndent + 4);
$expectedIndent = ($functionIndent + $this->indent);
}

// We changed lines, so this should be a whitespace indent token.
Expand All @@ -260,7 +265,7 @@ public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $st
}

if ($expectedIndent !== $foundIndent) {
$error = 'Multi-line function declaration not indented correctly; expected %s spaces but found %s';
$error = 'Multi-line function declaration not indented correctly; expected %s whitespace characters but found %s';
$data = array(
$expectedIndent,
$foundIndent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
class PSR1_Sniffs_Classes_ClassDeclarationSniff implements PHP_CodeSniffer_Sniff
{

/**
* Enforce namespaces for all classes?
* @var bool
*/
public $enforce_namespaces = true;

/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -62,7 +67,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$phpcsFile->addError($error, $nextClass, 'MultipleClasses');
}

if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
if ($this->enforce_namespaces && version_compare(PHP_VERSION, '5.3.0') >= 0) {
$namespace = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1));
if ($namespace === false) {
$error = 'Each class must be in a namespace of at least one level (a top-level vendor name)';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
class PSR2_Sniffs_ControlStructures_SwitchDeclarationSniff implements PHP_CodeSniffer_Sniff
{

/**
* Whitespace characters req'd per indent level
* @var int
*/
public $indent = 4;

/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -63,7 +68,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)

$switch = $tokens[$stackPtr];
$nextCase = $stackPtr;
$caseAlignment = ($switch['column'] + 4);
$caseAlignment = ($switch['column'] + $this->indent);
$caseCount = 0;
$foundDefault = false;

Expand All @@ -87,7 +92,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}

if ($tokens[$nextCase]['column'] !== $caseAlignment) {
$error = strtoupper($type).' keyword must be indented 4 spaces from SWITCH keyword';
$error = strtoupper($type).' keyword must be indented from SWITCH keyword';
$phpcsFile->addError($error, $nextCase, $type.'Indent');
}

Expand Down Expand Up @@ -123,7 +128,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
// Only need to check some things once, even if the
// closer is shared between multiple case statements, or even
// the default case.
if ($tokens[$nextCloser]['column'] !== ($caseAlignment + 4)) {
if ($tokens[$nextCloser]['column'] !== ($caseAlignment + $this->indent)) {
$error = 'Terminating statement must be indented to the same level as the CASE body';
$phpcsFile->addError($error, $nextCloser, 'BreakIndent');
}
Expand Down
37 changes: 37 additions & 0 deletions CodeSniffer/Standards/Snap/ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0"?>
<ruleset name="Snap">
<description>The Snap Interactive coding standard.</description>

<rule ref="PSR2"/>

<rule ref="PSR1.Classes.ClassDeclaration">
<properties>
<property name="enforce_namespaces" value="false"/>
</properties>
</rule>

<!-- Other nice things -->
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
<rule ref="Generic.Metrics.CyclomaticComplexity"/>
<rule ref="Generic.Metrics.NestingLevel"/>
<rule ref="Generic.Strings.UnnecessaryStringConcat">
<properties>
<property name="error" value="false"/>
</properties>
</rule>
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
<rule ref="Squiz.Classes.LowercaseClassKeywords"/>
<rule ref="Squiz.Commenting.FunctionCommentThrowTag"/>
<rule ref="Squiz.Functions.FunctionDuplicateArgument"/>
<rule ref="Squiz.PHP.DisallowMultipleAssignments"/>
<rule ref="Squiz.PHP.DiscouragedFunctions"/>
<rule ref="Squiz.PHP.GlobalKeyword"/>
<rule ref="Squiz.PHP.InnerFunctions"/>
<rule ref="Squiz.PHP.LowercasePHPFunctions"/>
<rule ref="Squiz.PHP.NonExecutableCode"/>
<rule ref="Squiz.Scope.StaticThisUsage"/>
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>

</ruleset>

0 comments on commit 7e04d48

Please sign in to comment.