Skip to content

MM-5069: [EQP][Sniffs Consolidation] Follow Magento coding standard #13

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

Merged
merged 1 commit into from
Jan 16, 2019
Merged
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ php:
install: composer install --no-interaction --prefer-source
script:
- bin/phpunit
- bin/phpcs --standard=PSR2 Magento/ --extensions=php
- bin/phpcs --standard=Magento Magento/ --extensions=php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function register()
*/
public function __construct()
{
// phpcs:ignore Magento.Classes.ObjectInstantiation.FoundDirectInstantiation
$this->annotationFormatValidator = new AnnotationFormatValidator();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MethodAnnotationStructureSniff implements Sniff
*/
public function __construct()
{
// phpcs:ignore Magento.Classes.ObjectInstantiation.FoundDirectInstantiation
$this->annotationFormatValidator = new AnnotationFormatValidator();
}

Expand Down
11 changes: 7 additions & 4 deletions Magento/Sniffs/Annotation/MethodArgumentsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ private function getMethodArguments(File $phpcsFile, $openParenthesisPtr, $close
private function getMethodParameters(array $paramDefinitions)
{
$paramName = [];
for ($i = 0; $i < count($paramDefinitions); $i++) {
$countParams = count($paramDefinitions);
for ($i = 0; $i < $countParams; $i++) {
if (isset($paramDefinitions[$i]['paramName'])) {
$paramName[] = $paramDefinitions[$i]['paramName'];
}
Expand Down Expand Up @@ -368,10 +369,11 @@ private function validateDuplicateAnnotationDoesnotExists(
$parametersCount = count($paramPointers);
if ($argumentsCount <= $parametersCount && $argumentsCount > 0) {
$duplicateParameters = [];
for ($i = 0; $i < sizeof($paramDefinitions); $i++) {
$countParams = count($paramDefinitions);
for ($i = 0; $i < $countParams; $i++) {
if (isset($paramDefinitions[$i]['paramName'])) {
$parameterContent = $paramDefinitions[$i]['paramName'];
for ($j = $i + 1; $j < count($paramDefinitions); $j++) {
for ($j = $i + 1; $j < $countParams; $j++) {
if (isset($paramDefinitions[$j]['paramName'])
&& $parameterContent === $paramDefinitions[$j]['paramName']
) {
Expand Down Expand Up @@ -507,7 +509,8 @@ private function validateMethodParameterAnnotations(
$phpcsFile,
$paramPointers
);
for ($ptr = 0; $ptr < count($methodArguments); $ptr++) {
$countArguments = count($methodArguments);
for ($ptr = 0; $ptr < $countArguments; $ptr++) {
$tokens = $phpcsFile->getTokens();
if (isset($paramPointers[$ptr])) {
$this->validateArgumentNameInParameterAnnotationExists(
Expand Down
3 changes: 2 additions & 1 deletion Magento/Sniffs/Classes/ObjectInstantiationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class ObjectInstantiationSniff implements Sniff
*
* @var string
*/
protected $warningMessage = 'Direct object instantiation (object of %s) is discouraged in Magento 2.';
// phpcs:ignore Magento.Files.LineLength.MaxExceeded
protected $warningMessage = 'Direct %s object instantiation is discouraged in Magento. Use dependency injection or factories instead.';

/**
* Warning violation code.
Expand Down
5 changes: 2 additions & 3 deletions Magento/Sniffs/Classes/ObjectManagerSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use PHP_CodeSniffer\Files\File;

/**
* Class ObjectManagerSniff
* Detects direct ObjectManager usage.
* Class ObjectManagerSniff detects direct ObjectManager usage.
*/
class ObjectManagerSniff implements Sniff
{
Expand All @@ -19,7 +18,7 @@ class ObjectManagerSniff implements Sniff
*
* @var string
*/
// phpcs:ignore Generic.Files.LineLength.TooLong
// phpcs:ignore Magento.Files.LineLength.MaxExceeded
protected $warningMessage = 'The direct use of ObjectManager is discouraged. Inject necessary dependencies via constructor.';

/**
Expand Down
2 changes: 1 addition & 1 deletion Magento/Sniffs/PHP/DateTimeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DateTimeSniff implements Sniff
*
* @var string
*/
// phpcs:ignore Generic.Files.LineLength.TooLong
// phpcs:ignore Magento.Files.LineLength.MaxExceeded
protected $warningMessage = 'Overcomplicated Date/Time handling. Use \Magento\Framework\Stdlib\DateTime\TimezoneInterface instead.';

/**
Expand Down
2 changes: 1 addition & 1 deletion Magento/Sniffs/PHP/FinalImplementationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function register()
public function process(File $phpcsFile, $stackPtr)
{
$phpcsFile->addError(
// phpcs:ignore Generic.Files.LineLength.TooLong
// phpcs:ignore Magento.Files.LineLength.MaxExceeded
'Final keyword is prohibited in Magento. It decreases extensibility and is not compatible with plugins and proxies.',
$stackPtr,
'FoundFinal'
Expand Down
4 changes: 4 additions & 0 deletions Magento/Sniffs/PHP/LiteralNamespacesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function process(File $sourceFile, $stackPtr)
}

/**
* Checks if class or interface exists.
*
* ToDo: get rig of this check https://github.com/magento/magento-coding-standard/issues/9
*
* @param string $className
* @return bool
*/
Expand Down
8 changes: 4 additions & 4 deletions Magento/Sniffs/Performance/EmptyCheckSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class EmptyCheckSniff implements Sniff
*/
protected $map = [
'count' => [
// phpcs:ignore Generic.Files.LineLength.TooLong
// phpcs:ignore Magento.Files.LineLength.MaxExceeded
'message' => 'count(...) function should not be used to check if array is empty. Use empty(...) language construct instead',
'code' => 'FoundCount'
],
'strlen' => [
// phpcs:ignore Generic.Files.LineLength.TooLong
// phpcs:ignore Magento.Files.LineLength.MaxExceeded
'message' => 'strlen(...) function should not be used to check if string is empty. Consider replace with (... === "") or (... !== "")',
'code' => 'FoundStrlen'
],
Expand Down Expand Up @@ -114,7 +114,7 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->addWarning($message, $stackPtr, $code);
}
} else {
// phpcs:ignore Generic.Files.LineLength.TooLong
// phpcs:ignore Magento.Files.LineLength.MaxExceeded
if ($phpcsFile->findNext($this->otherComparisonOperators, $functionPosition, $endOfStatementPosition) === false) {
$phpcsFile->addWarning($message, $stackPtr, $code);
}
Expand All @@ -130,7 +130,7 @@ public function process(File $phpcsFile, $stackPtr)
*/
private function findFunctionPosition($index)
{
// phpcs:ignore Generic.Files.LineLength.TooLong
// phpcs:ignore Magento.Files.LineLength.MaxExceeded
for ($i = $this->tokens[$index]['parenthesis_opener'] + 1; $i < $this->tokens[$index]['parenthesis_closer']; $i++) {
if (array_key_exists($this->tokens[$i]['content'], $this->map)) {
return $i;
Expand Down
2 changes: 2 additions & 0 deletions Magento/Sniffs/Security/IncludeFileSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public function register()

/**
* @inheritdoc
* phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
*/
public function process(File $phpcsFile, $stackPtr)
{
// phpcs:enable
$tokens = $phpcsFile->getTokens();
$firstToken = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
$message = '"%s" statement detected. File manipulations are discouraged.';
Expand Down
2 changes: 1 addition & 1 deletion Magento/Sniffs/Security/LanguageConstructSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LanguageConstructSniff implements Sniff
*
* @var string
*/
// phpcs:ignore Generic.Files.LineLength.TooLong
// phpcs:ignore Magento.Files.LineLength.MaxExceeded
protected $errorMessageBacktick = 'Incorrect usage of back quote string constant. Back quotes should be always inside strings.';

/**
Expand Down
3 changes: 3 additions & 0 deletions Magento/Sniffs/Security/XssTemplateSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ private function findSpecialAnnotation($stackPtr)
*
* @param array $statement
* @return void
*
* phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
*/
private function detectUnescapedString($statement)
{
// phpcs:enable
$posOfFirstElement = $this->file->findNext(
[T_WHITESPACE, T_COMMENT],
$statement['start'],
Expand Down
2 changes: 1 addition & 1 deletion Magento/Sniffs/Strings/ExecutableRegExSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ExecutableRegExSniff implements Sniff
*
* @var string
*/
// phpcs:ignore Generic.Files.LineLength.TooLong
// phpcs:ignore Magento.Files.LineLength.MaxExceeded
protected $errorMessage = "Possible executable regular expression in %s. Make sure that the pattern doesn't contain 'e' modifier";

/**
Expand Down
8 changes: 1 addition & 7 deletions Magento/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod">
<severity>6</severity>
</rule>
<rule ref="Generic.Commenting.Fixme">
<severity>6</severity>
<type>warning</type>
</rule>
<rule ref="Generic.Commenting.Todo">
<severity>6</severity>
</rule>
<rule ref="Generic.Functions.CallTimePassByReference">
<severity>10</severity>
</rule>
Expand Down Expand Up @@ -84,6 +77,7 @@
<property name="lineLimit" value="120"/>
<property name="absoluteLineLimit" value="120"/>
</properties>
<type>warning</type>
<severity>6</severity>
</rule>
<rule ref="Magento.Legacy.MageEntity">
Expand Down