Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit c24882a

Browse files
committed
Updated rulesets dir names, added Wordpress custom rules to pass _jmvt theme code
1 parent b1554ca commit c24882a

File tree

8 files changed

+370
-16
lines changed

8 files changed

+370
-16
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.DS_Store
1+
.DS_Store
2+
.idea

justcoded/JustCoded-Wordpress/ruleset.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

justcoded/JustCoded-PSR2/ruleset.xml renamed to justcoded/JustcodedPSR2/ruleset.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0"?>
2-
<ruleset name="JustCoded-PSR2">
2+
<ruleset name="JustcodedPSR2">
33
<description>The PSR-2 coding standard with Tabs identation.</description>
44

5-
<!-- Include the whole PSR-2 standard -->
6-
<rule ref="PSR2">
7-
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
8-
<exclude name="Generic.ControlStructures.InlineControlStructure"/>
9-
</rule>
5+
<!-- Include the whole PSR-2 standard -->
6+
<rule ref="PSR2">
7+
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
8+
<exclude name="Generic.ControlStructures.InlineControlStructure"/>
9+
</rule>
1010

1111
<!-- Covers rule: Your indentation should always reflect logical structure. -->
1212
<rule ref="Generic.WhiteSpace.ScopeIndent">
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* WordPress Coding Standard.
4+
*
5+
* @package WPCS\WordPressCodingStandards
6+
* @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
7+
* @license https://opensource.org/licenses/MIT MIT
8+
*/
9+
10+
/**
11+
* Ensures filenames do not contain underscores.
12+
*
13+
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#naming-conventions
14+
*
15+
* @package WPCS\WordPressCodingStandards
16+
*
17+
* @since 0.1.0
18+
*/
19+
class JustcodedWordpress_Sniffs_Files_FileNameSniff implements PHP_CodeSniffer_Sniff
20+
{
21+
22+
/**
23+
* Returns an array of tokens this test wants to listen for.
24+
*
25+
* @return array
26+
*/
27+
public function register()
28+
{
29+
return array(T_OPEN_TAG);
30+
}
31+
32+
/**
33+
* Processes this test, when one of its tokens is encountered.
34+
*
35+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
36+
* @param int $stackPtr The position of the current token in the
37+
* stack passed in $tokens.
38+
*
39+
* @return int
40+
*/
41+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
42+
{
43+
$fileName = basename($phpcsFile->getFileName());
44+
45+
if (0 < strpos($fileName, '_')) {
46+
$expected = str_replace('_', '-', $fileName);
47+
if (0 == strpos($fileName, '_')) {
48+
$expected = '_' . substr($expected, 1);
49+
}
50+
51+
$error = 'Filename "' . $fileName . '" with underscores found; use ' . $expected . ' instead';
52+
$phpcsFile->addError($error, $stackPtr, 'UnderscoresNotAllowed');
53+
}
54+
55+
return ($phpcsFile->numTokens + 1);
56+
}
57+
58+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* JustCoded_Sniffs_WordPress_Files_LowercasedFilenameSniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @author Alex Prokopenko <[email protected]>
9+
*/
10+
11+
/**
12+
* Checks that all file names are lowercased.
13+
* Ignore classes which follows PSR autoload format.
14+
*/
15+
class JustcodedWordpress_Sniffs_Files_LowercasedFilenameSniff implements PHP_CodeSniffer_Sniff
16+
{
17+
/**
18+
* Returns an array of tokens this test wants to listen for.
19+
*
20+
* @return array
21+
*/
22+
public function register()
23+
{
24+
return array(T_OPEN_TAG);
25+
}
26+
27+
/**
28+
* Processes this sniff, when one of its tokens is encountered.
29+
*
30+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
31+
* @param int $stackPtr The position of the current token in
32+
* the stack passed in $tokens.
33+
*
34+
* @return int
35+
*/
36+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
37+
{
38+
$filename = $phpcsFile->getFilename();
39+
if ($filename === 'STDIN') {
40+
return;
41+
}
42+
43+
$filename = basename($filename);
44+
$lowercaseFilename = strtolower($filename);
45+
if ($filename !== $lowercaseFilename) {
46+
if (!$this->checkAutoloadNamespaceClass($phpcsFile, $stackPtr)) {
47+
$data = array(
48+
$filename,
49+
$lowercaseFilename,
50+
);
51+
$error = 'Filename "%s" doesn\'t match the expected filename "%s"';
52+
$phpcsFile->addError($error, $stackPtr, 'NotFound', $data);
53+
$phpcsFile->recordMetric($stackPtr, 'Lowercase filename', 'no');
54+
} else {
55+
$phpcsFile->recordMetric($stackPtr, 'Lowercase filename', 'no. psr-0 autoload detected');
56+
}
57+
} else {
58+
$phpcsFile->recordMetric($stackPtr, 'Lowercase filename', 'yes');
59+
}
60+
61+
// Ignore the rest of the file.
62+
return ($phpcsFile->numTokens + 1);
63+
}
64+
65+
/**
66+
* Check that filename has namespace and class name is the same as filename.
67+
* This means we have PSR0/PSR4 autoload enabled.
68+
*
69+
* @param PHP_CodeSniffer_File $phpcsFile
70+
* @param int $stackPtr
71+
*
72+
* @return bool
73+
*/
74+
protected function checkAutoloadNamespaceClass(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
75+
{
76+
$filename = $phpcsFile->getFilename();
77+
$tokens = $phpcsFile->getTokens();
78+
79+
$in_namespace = false;
80+
$findTokens = array(
81+
T_CLASS,
82+
T_INTERFACE,
83+
T_TRAIT,
84+
T_NAMESPACE,
85+
T_CLOSE_TAG,
86+
);
87+
88+
$stackPtr = $phpcsFile->findNext($findTokens, ($stackPtr + 1));
89+
while ($stackPtr !== false) {
90+
if ($tokens[$stackPtr]['code'] === T_CLOSE_TAG) {
91+
// if we reach here this means that we can't find namespace and class/interface/trait.
92+
return false;
93+
}
94+
95+
// Keep track of what namespace we are in.
96+
if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
97+
$in_namespace = true;
98+
} else {
99+
// we're in declaration of class/interface/trait
100+
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
101+
$name = $tokens[$nameToken]['content'];
102+
103+
$name_from_file = preg_replace('/\.php$/i', '', basename($filename));
104+
if (0 === strcmp($name_from_file, $name) && $in_namespace) {
105+
return true;
106+
} else {
107+
return false;
108+
}
109+
}
110+
111+
$stackPtr = $phpcsFile->findNext($findTokens, ($stackPtr + 1));
112+
}
113+
114+
return false;
115+
}
116+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* WordPress Coding Standard.
4+
*
5+
* @package WPCS\WordPressCodingStandards
6+
* @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
7+
* @license https://opensource.org/licenses/MIT MIT
8+
*/
9+
10+
if ( ! class_exists( 'WordPress_Sniffs_NamingConventions_ValidVariableNameSniff', true ) ) {
11+
throw new PHP_CodeSniffer_Exception( 'Class WordPress_Sniffs_NamingConventions_ValidVariableNameSniff not found' );
12+
}
13+
14+
/**
15+
* Checks the naming of variables and member variables.
16+
*
17+
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#naming-conventions
18+
*
19+
* @package WPCS\WordPressCodingStandards
20+
*
21+
* @since 0.9.0
22+
*
23+
* Last synced with base class July 2014 at commit ed257ca0e56ad86cd2a4d6fa38ce0b95141c824f.
24+
* @link https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php
25+
*/
26+
class JustcodedWordpress_Sniffs_NamingConventions_ValidVariableNameSniff extends WordPress_Sniffs_NamingConventions_ValidVariableNameSniff
27+
{
28+
29+
/**
30+
* Custom list of variables which can have mixed case.
31+
*
32+
* @since 0.10.0
33+
*
34+
* @var string[]
35+
*/
36+
public $customVariablesWhitelist = array(
37+
'SLUG',
38+
'TITLE',
39+
);
40+
41+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* WordPress Coding Standard, Justcoded edition.
4+
*/
5+
6+
if ( ! class_exists( 'WordPress_Sniffs_XSS_EscapeOutputSniff', true ) ) {
7+
throw new PHP_CodeSniffer_Exception( 'Class WordPress_Sniffs_XSS_EscapeOutputSniff not found' );
8+
}
9+
10+
/**
11+
* Verifies that all outputted strings are escaped.
12+
*
13+
* @link http://codex.wordpress.org/Data_Validation Data Validation on WordPress Codex
14+
*/
15+
class JustcodedWordpress_Sniffs_XSS_EscapeOutputSniff extends WordPress_Sniffs_XSS_EscapeOutputSniff {
16+
17+
/**
18+
* Custom list of functions which escape values for output.
19+
*
20+
* @since 0.5.0
21+
*
22+
* @var string[]
23+
*/
24+
public $customEscapingFunctions = array();
25+
26+
/**
27+
* Custom list of functions whose return values are pre-escaped for output.
28+
*
29+
* @since 0.3.0
30+
*
31+
* @var string[]
32+
*/
33+
public $customAutoEscapedFunctions = array(
34+
'cpt_prev_posts_link',
35+
'cpt_next_posts_link',
36+
'assets',
37+
'parent_assets',
38+
);
39+
40+
/**
41+
* Custom list of functions which escape values for output.
42+
*
43+
* @since 0.3.0
44+
* @deprecated 0.5.0 Use $customEscapingFunctions instead.
45+
* @see WordPress_Sniffs_XSS_EscapeOutputSniff::$customEscapingFunctions
46+
*
47+
* @var string[]
48+
*/
49+
public $customSanitizingFunctions = array();
50+
51+
/**
52+
* Custom list of functions which print output incorporating the passed values.
53+
*
54+
* @since 0.4.0
55+
*
56+
* @var string[]
57+
*/
58+
public $customPrintingFunctions = array();
59+
60+
/**
61+
* Processes this test, when one of its tokens is encountered.
62+
*
63+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
64+
* @param int $stackPtr The position of the current token
65+
* in the stack passed in $tokens.
66+
*
67+
* @return int|void
68+
*/
69+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
70+
{
71+
$originStackPtr = $stackPtr;
72+
73+
$filename = $phpcsFile->getFilename();
74+
$layout_view_pattern = 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR;
75+
$this->tokens = $phpcsFile->getTokens();
76+
77+
if (false !== strpos($filename, $layout_view_pattern)
78+
&& T_ECHO == $this->tokens[$stackPtr]['code']
79+
) {
80+
$next = $phpcsFile->findNext(
81+
PHP_CodeSniffer_Tokens::$emptyTokens,
82+
( $stackPtr + 1 ),
83+
null,
84+
true,
85+
null,
86+
true
87+
);
88+
if ( T_VARIABLE == $this->tokens[ $next ]['code'] && '$content' === $this->tokens[ $next ]['content'] ) {
89+
return;
90+
}
91+
}
92+
93+
parent::process($phpcsFile, $originStackPtr);
94+
}
95+
96+
}

0 commit comments

Comments
 (0)