Skip to content

#24: [New Rule] Static methods SHOULD NOT be used #41

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 3 commits into from
Feb 27, 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
59 changes: 59 additions & 0 deletions Magento/Sniffs/Functions/StaticFunctionSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sniffs\Functions;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;

/**
* Detects static function definitions.
*/
class StaticFunctionSniff implements Sniff
{
/**
* String representation of warning.
*
* @var string
*/
protected $warningMessage = 'Static method cannot be intercepted and its use is discouraged.';

/**
* Warning violation code.
*
* @var string
*/
protected $warningCode = 'StaticFunction';

/**
* @inheritDoc
*/
public function register()
{
return [T_STATIC];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$posOfFunction = $phpcsFile->findNext(T_FUNCTION, $stackPtr) + 1;
$tokens = array_slice($phpcsFile->getTokens(), $stackPtr, $posOfFunction - $stackPtr);

$allowedTypes = [T_STATIC => true, T_WHITESPACE => true, T_FUNCTION => true];
foreach ($tokens as $token) {
$code = $token['code'];
if (!array_key_exists($code, $allowedTypes)) {
break;
}

if ($code === T_FUNCTION) {
$phpcsFile->addWarning($this->warningMessage, $posOfFunction, $this->warningCode);
}
}
}
}
26 changes: 26 additions & 0 deletions Magento/Tests/Functions/StaticFunctionUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Foo\Bar;

interface FooInterface
{
public static
function aStaticMethod();

public function normalMethod();
}

class Foo implements FooInterface
{
private static $property;

public static function aStaticMethod()
{
return self::$property;
}

public function normalMethod()
{
return $this;
}
}
33 changes: 33 additions & 0 deletions Magento/Tests/Functions/StaticFunctionUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Tests\Functions;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Class StaticFunctionSniffTest
*/
class StaticFunctionUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
protected function getErrorList()
{
return [];
}

/**
* @inheritdoc
*/
protected function getWarningList()
{
return [
8 => 1,
17 => 1
];
}
}
3 changes: 3 additions & 0 deletions Magento/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<rule ref="Magento.Exceptions.Namespace">
<severity>8</severity>
</rule>
<rule ref="Magento.Functions.StaticFunction">
<severity>6</severity>
</rule>
<rule ref="Magento.Files.LineLength">
<properties>
<property name="lineLimit" value="120"/>
Expand Down