Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PES-1486_phpcs_rules_improvement #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie.BraceOnNewLine" />
<!-- don't allow opening class brace on new line -->
<rule ref="Generic.Classes.OpeningBraceSameLine.BraceOnNewLine" />
<!-- if file has file comment, it must have 1 blank line after the comment -->
<rule ref="Squiz.Commenting.FileComment.SpacingAfterOpen" />
<rule ref="Squiz.Commenting.FileComment.SpacingAfterComment" />

<!-- Rules taken from Slevomat coding standard -->

Expand All @@ -86,6 +89,33 @@
<property name="ignoreSpacesInComments" value="true"/>
</properties>
</rule>
<!-- force constructor parameters to be on multiple lines if more than 1 parameter exists -->
<rule ref="./phpcs/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/RequireMultiLineMethodSignatureSniff.php">
<properties>
<property name="minParametersCount" value="2"/>
<property name="includedMethodPatterns" type="array">
<element value="~__construct~"/>
</property>
</properties>
</rule>
<!-- require 1 blank line between different types of class members -->
<rule ref="./phpcs/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/ClassMemberSpacingSniff.php" />
<!-- disallow blank lines after class opening brace and before class closing brace -->
<rule ref="./phpcs/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/EmptyLinesAroundClassBracesSniff.php">
<properties>
<property name="linesCountAfterOpeningBrace" value="0"/>
<property name="linesCountBeforeClosingBrace" value="0"/>
</properties>
</rule>
<!-- disallow blank lines between class constants that have annotation, allow max 1 blank line between class constants without annotation-->
<rule ref="./phpcs/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/ConstantSpacingSniff.php">
<properties>
<property name="minLinesCountBeforeWithComment" value="0"/>
<property name="maxLinesCountBeforeWithComment" value="0"/>
<property name="minLinesCountBeforeWithoutComment" value="0"/>
<property name="maxLinesCountBeforeWithoutComment" value="1"/>
</properties>
</rule>

<!-- Packeta custom rules -->

Expand Down
10 changes: 8 additions & 2 deletions phpcs/Packetery/Sniffs/Annotations/MethodAnnotationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function process(File $phpcsFile, $stackPtr) {
$annotationFound = false;
foreach ($paramAnnotations as $paramAnnotation) {
if ($paramAnnotation->getName() === '@param'
&& $paramAnnotation->getParameterName() === $paramName) {
&& $paramAnnotation->getValue()->parameterName === $paramName) {
$annotationFound = true;
break;
}
Expand All @@ -55,14 +55,20 @@ public function process(File $phpcsFile, $stackPtr) {
}

$missingAnnotations = [];
$presentAnnotationNames = [];
foreach ($annotations as $annotation) {
$presentAnnotationNames[] = $annotation->getName();
}

foreach (self::REQUIRED_ANNOTATIONS as $requiredAnnotation) {
if ($requiredAnnotation === '@return' && $methodName === '__construct') {
continue;
}
if (!isset($annotations[$requiredAnnotation])) {
if (!in_array($requiredAnnotation, $presentAnnotationNames, true)) {
$missingAnnotations[] = $requiredAnnotation;
}
}

if (!empty($missingAnnotations)) {
$phpcsFile->addError(
sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use SlevomatCodingStandard\Helpers\PropertyHelper;

class PropertyAnnotationSniff implements Sniff {

/**
* @return array
*/
Expand All @@ -26,7 +25,12 @@ public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
if (PropertyHelper::isProperty($phpcsFile, $stackPtr)) {
$annotations = AnnotationHelper::getAnnotations($phpcsFile, $stackPtr);
if (empty($annotations) || !isset($annotations['@var'])) {
$presentAnnotationNames = [];
foreach ($annotations as $annotation) {
$presentAnnotationNames[] = $annotation->getName();
}

if (!in_array('@var', $presentAnnotationNames, true)) {
$propertyName = $tokens[$stackPtr]['content'];
$phpcsFile->addError(
sprintf('Property %s is missing @var annotation', $propertyName),
Expand Down
2 changes: 1 addition & 1 deletion phpcs/composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"require-dev": {
"squizlabs/php_codesniffer": "3.*",
"slevomat/coding-standard": "8.0"
"slevomat/coding-standard": "8.*"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nepovoluj prosím jakoukoli verzi 8.x. Může se stát, že si vývojář aktualizuje novější verzi a některé sniffy mu přestanou fungovat - tak jak se to stalo tobě. Zvol konkrétní verzi (která je nyní nejnovější).

},
"config": {
"allow-plugins": {
Expand Down