Skip to content

Commit 8decfcf

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/psr'
2 parents da7e755 + 332f65e commit 8decfcf

14 files changed

+1188
-74
lines changed

CHANGELOG.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
# Changelog
22

3-
## 0.2.0 (NOT RELEASED)
3+
## 0.3.0 (NOT RELEASED)
4+
* Usage of PSR 1/2 as base
5+
* Test for PHP 7+
6+
* Introduction of [phpcs-variable-analysis](https://github.com/sirbrillig/phpcs-variable-analysis)
7+
* Introduction of [Autmattic NeutronStandard](https://github.com/Automattic/phpcs-neutron-standard)
8+
* Only use few WordPress rules from [wpcs](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards)
9+
* Make package compatible with `phpcodesniffer-composer-installer`
10+
* Add lot of info to README
11+
12+
## 0.2.0
413
* Removed `Generic.PHP.LowerCaseConstant`, because we're going to use PSR standards.
514
* Updated to newer version of `wp-coding-standards/wpcs`.
615
* Updated to newer version of `squizlabs/php_codesniffer`.
716
* Added support for PHP7+.
817
* Added new excludes which are too WordPressy.
918

1019
## 0.1.0
11-
* First release.
20+
* First release.

Inpsyde/Helpers.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php declare(strict_types=1); # -*- coding: utf-8 -*-
2+
/*
3+
* This file is part of the php-coding-standards package.
4+
*
5+
* (c) Inpsyde GmbH
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* This file contains code from "phpcs-calisthenics-rules" repository
11+
* found at https://github.com/object-calisthenics
12+
* Copyright (c) 2014 Doctrine Project
13+
* released under MIT license.
14+
*/
15+
16+
namespace Inpsyde\CodingStandard;
17+
18+
use PHP_CodeSniffer\Files\File;
19+
20+
/**
21+
* @package php-coding-standards
22+
* @license http://opensource.org/licenses/MIT MIT
23+
*/
24+
class Helpers
25+
{
26+
const CODE_TO_TYPE_MAP = [
27+
T_CONST => 'Constant',
28+
T_CLASS => 'Class',
29+
T_FUNCTION => 'Function',
30+
T_TRAIT => 'Trait',
31+
];
32+
33+
/**
34+
* @param File $file
35+
* @param int $position
36+
* @return mixed[]
37+
*/
38+
public static function classPropertiesTokenIndexes(
39+
File $file,
40+
int $position
41+
): array {
42+
$tokens = $file->getTokens();
43+
$token = $tokens[$position] ?? [];
44+
45+
if (!array_key_exists('scope_opener', $token)
46+
|| !array_key_exists('scope_closer', $token)
47+
) {
48+
return [];
49+
}
50+
51+
$propertyList = [];
52+
$pointer = (int)$token['scope_opener'];
53+
54+
while ($pointer) {
55+
if (self::isProperty($file, $pointer)) {
56+
$propertyList[] = $pointer;
57+
}
58+
$pointer = (int)$file->findNext(
59+
T_VARIABLE,
60+
($pointer + 1),
61+
$token['scope_closer']
62+
);
63+
}
64+
65+
return $propertyList;
66+
}
67+
68+
/**
69+
* @param File $file
70+
* @param int $variablePosition
71+
* @return bool
72+
*/
73+
public static function isProperty(File $file, int $variablePosition): bool
74+
{
75+
$propertyPointer = $file->findPrevious(
76+
[T_STATIC, T_WHITESPACE, T_COMMENT],
77+
$variablePosition - 1,
78+
null,
79+
true
80+
);
81+
82+
$propertyPointerToken = $file->getTokens()[$propertyPointer] ?? [];
83+
84+
return in_array(
85+
($propertyPointerToken['code'] ?? ''),
86+
[T_PRIVATE, T_PROTECTED, T_PUBLIC, T_VAR],
87+
true
88+
);
89+
}
90+
91+
/**
92+
* @param File $file
93+
* @param int $position
94+
* @return string
95+
*/
96+
public static function tokenTypeName(File $file, int $position): string
97+
{
98+
$token = $file->getTokens()[$position];
99+
$tokenCode = $token['code'];
100+
if (isset(self::CODE_TO_TYPE_MAP[$tokenCode])) {
101+
return self::CODE_TO_TYPE_MAP[$tokenCode];
102+
}
103+
104+
if ($token['code'] === T_VARIABLE) {
105+
if (self::isProperty($file, $position)) {
106+
return 'Property';
107+
}
108+
109+
return 'Variable';
110+
}
111+
112+
return '';
113+
}
114+
115+
/**
116+
* @param File $file
117+
* @param int $position
118+
* @return string
119+
*/
120+
public static function tokenName(File $file, int $position): string
121+
{
122+
$name = $file->getTokens()[$position]['content'] ?? '';
123+
124+
if (strpos($name, '$') === 0) {
125+
return trim($name, '$');
126+
}
127+
128+
$namePosition = $file->findNext(T_STRING, $position, $position + 3);
129+
130+
return $file->getTokens()[$namePosition]['content'];
131+
}
132+
}

Inpsyde/PHPCSAliases.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* PHPCS cross-version compatibility helper.
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+
* @since 0.13.0
9+
*/
10+
11+
/*
12+
* Alias a number of PHPCS 3.x classes to their PHPCS 2.x equivalents.
13+
*
14+
* This file is auto-loaded by PHPCS 3.x before any sniffs are loaded
15+
* through the PHPCS 3.x `<autoload>` ruleset directive.
16+
*
17+
* {@internal The PHPCS files have been reorganized in PHPCS 3.x, quite
18+
* a few "old" classes have been split and spread out over several "new"
19+
* classes. In other words, this will only work for a limited number
20+
* of classes.}}
21+
*
22+
* {@internal The `class_exists` wrappers are needed to play nice with other
23+
* external PHPCS standards creating cross-version compatibility in the same
24+
* manner.}}
25+
*/
26+
if (!defined('WPCS_PHPCS_ALIASES_SET')) {
27+
// PHPCS base classes/interface.
28+
if (!interface_exists('\PHP_CodeSniffer_Sniff')) {
29+
class_alias('PHP_CodeSniffer\Sniffs\Sniff', '\PHP_CodeSniffer_Sniff');
30+
}
31+
if (!class_exists('\PHP_CodeSniffer_File')) {
32+
class_alias('PHP_CodeSniffer\Files\File', '\PHP_CodeSniffer_File');
33+
}
34+
if (!class_exists('\PHP_CodeSniffer_Tokens')) {
35+
class_alias('PHP_CodeSniffer\Util\Tokens', '\PHP_CodeSniffer_Tokens');
36+
}
37+
38+
// PHPCS classes which are being extended by WPCS sniffs.
39+
if (!class_exists('\PHP_CodeSniffer_Standards_AbstractVariableSniff')) {
40+
class_alias('PHP_CodeSniffer\Sniffs\AbstractVariableSniff',
41+
'\PHP_CodeSniffer_Standards_AbstractVariableSniff');
42+
}
43+
if (!class_exists('\PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff')) {
44+
class_alias('PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions\ValidFunctionNameSniff',
45+
'\PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff');
46+
}
47+
if (!class_exists('\Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff')) {
48+
class_alias('PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\OperatorSpacingSniff',
49+
'\Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff');
50+
}
51+
if (!class_exists('\Squiz_Sniffs_WhiteSpace_SemicolonSpacingSniff')) {
52+
class_alias('PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SemicolonSpacingSniff',
53+
'\Squiz_Sniffs_WhiteSpace_SemicolonSpacingSniff');
54+
}
55+
56+
define('WPCS_PHPCS_ALIASES_SET', true);
57+
58+
/*
59+
* Register our own autoloader for the WPCS abstract classes & the helper class.
60+
*
61+
* This can be removed once the minimum required version of WPCS for the
62+
* PHPCS 3.x branch has gone up to 3.1.0 (unreleased as of yet) or
63+
* whichever version contains the fix for upstream #1591.
64+
*
65+
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/1564
66+
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/1591
67+
*/
68+
spl_autoload_register(function ($class) {
69+
// Only try & load our own classes.
70+
if (stripos($class, 'WordPress') !== 0) {
71+
return;
72+
}
73+
74+
// PHPCS handles the Test and Sniff classes without problem.
75+
if (stripos($class, '\Tests\\') !== false || stripos($class,
76+
'\Sniffs\\') !== false) {
77+
return;
78+
}
79+
80+
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR . strtr($class, '\\',
81+
DIRECTORY_SEPARATOR) . '.php';
82+
83+
if (file_exists($file)) {
84+
include_once $file;
85+
}
86+
});
87+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php declare(strict_types=1); # -*- coding: utf-8 -*-
2+
/*
3+
* This file is part of the php-coding-standards package.
4+
*
5+
* (c) Inpsyde GmbH
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* This file contains code from "phpcs-calisthenics-rules" repository
11+
* found at https://github.com/object-calisthenics
12+
* Copyright (c) 2014 Doctrine Project
13+
* released under MIT license.
14+
*/
15+
16+
namespace Inpsyde\CodingStandard\Sniffs\CodeQuality;
17+
18+
use Inpsyde\CodingStandard\Helpers;
19+
use PHP_CodeSniffer\Files\File;
20+
use PHP_CodeSniffer\Sniffs\Sniff;
21+
22+
/**
23+
* @package php-coding-standards
24+
* @license http://opensource.org/licenses/MIT MIT
25+
*/
26+
final class ElementNameMinimalLengthSniff implements Sniff
27+
{
28+
29+
/**
30+
* @var int
31+
*/
32+
public $minLength = 3;
33+
34+
/**
35+
* @var string[]
36+
*/
37+
public $allowedShortNames = ['i', 'id', 'to', 'up', 'ok', 'no', 'go', 'it'];
38+
39+
/**
40+
* @return int[]
41+
*/
42+
public function register(): array
43+
{
44+
return [T_CLASS, T_TRAIT, T_INTERFACE, T_CONST, T_FUNCTION, T_VARIABLE];
45+
}
46+
47+
/**
48+
* @param File $file
49+
* @param int $position
50+
*/
51+
public function process(File $file, $position)
52+
{
53+
$elementName = Helpers::tokenName($file, $position);
54+
$elementNameLength = mb_strlen($elementName);
55+
56+
if ($this->shouldBeSkipped($elementNameLength, $elementName)) {
57+
return;
58+
}
59+
60+
$typeName = Helpers::tokenTypeName($file, $position);
61+
$message = sprintf(
62+
'%s name "%s" is only %d chars long. Must be at least %d.',
63+
$typeName,
64+
$elementName,
65+
$elementNameLength,
66+
$this->minLength
67+
);
68+
69+
$file->addError($message, $position, 'ElementNameMinimalLength');
70+
}
71+
72+
/**
73+
* @param int $elementNameLength
74+
* @param string $elementName
75+
* @return bool
76+
*/
77+
private function shouldBeSkipped(
78+
int $elementNameLength,
79+
string $elementName
80+
): bool {
81+
if ($elementNameLength >= $this->minLength) {
82+
return true;
83+
}
84+
85+
if ($this->isShortNameAllowed($elementName)) {
86+
return true;
87+
}
88+
89+
return false;
90+
}
91+
92+
/**
93+
* @param string $variableName
94+
* @return bool
95+
*/
96+
private function isShortNameAllowed(string $variableName): bool
97+
{
98+
return in_array($variableName, $this->allowedShortNames);
99+
}
100+
}

0 commit comments

Comments
 (0)