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

phpcs 3.0 refactor #32

Closed
wants to merge 9 commits into from
Closed
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
14 changes: 10 additions & 4 deletions Yii2/Sniffs/Files/SpacesAroundConcatSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,31 @@
*
* @since 2.0.2
*/
class Yii2_Sniffs_Files_SpacesAroundConcatSniff implements PHP_CodeSniffer_Sniff

namespace PHP_CodeSniffer\Standards\Yii2\Sniffs\Files;

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

class SpacesAroundConcatSniff implements Sniff
{
public function register()
{
return [T_STRING_CONCAT];
}

public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if ($tokens[$stackPtr-1]['type'] !== 'T_WHITESPACE') {
if ($tokens[$stackPtr - 1]['type'] !== 'T_WHITESPACE') {
$error = 'Whitespace is expected before any concat operator "."';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpace');
if ($fix === true) {
$phpcsFile->fixer->addContentBefore($stackPtr, ' ');
}
}
if ($tokens[$stackPtr+1]['type'] !== 'T_WHITESPACE') {
if ($tokens[$stackPtr + 1]['type'] !== 'T_WHITESPACE') {
$error = 'Whitespace is expected after any concat operator "."';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpace');
if ($fix === true) {
Expand Down
23 changes: 17 additions & 6 deletions Yii2/Sniffs/Properties/PrivatePropertiesUnderscoreSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,34 @@
* The private property name is identified by the PHP token T_VARIABLE
* The private visibility is identified by the PHP token T_PRIVATE
*/
class Yii2_Sniffs_Properties_PrivatePropertiesUnderscoreSniff implements PHP_CodeSniffer_Sniff

namespace PHP_CodeSniffer\Standards\Yii2\Sniffs\Files;

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

class PrivatePropertiesUnderscoreSniff implements Sniff
{
public function register()
{
return [T_PRIVATE];
}

public function process(PHP_CodeSniffer_File $file, $pointer)
public function process(File $file, $pointer)
{
$tokens = $file->getTokens();
if ($tokens[$pointer]['content'] === 'private' &&
$tokens[$pointer + 1]['type'] === 'T_WHITESPACE' &&
$tokens[$pointer + 2]['type'] === 'T_VARIABLE' &&
strpos($tokens[$pointer + 2]['content'], '$_') !== 0) {

$data = [$tokens[$pointer + 2]['content']];
$file->addError('Private property name "%s" must be prefixed with underscore.', $pointer, 'NoUnderscore', $data);
strpos($tokens[$pointer + 2]['content'], '$_') !== 0
) {
$data = [$tokens[$pointer + 2]['content']];
$file->addError(
'Private property name "%s" must be prefixed with underscore.',
$pointer,
'NoUnderscore',
$data
);
}
}
}
5 changes: 5 additions & 0 deletions Yii2/Tests/Files/SpacesAroundConcatUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$a = $b.$c;
$a = $b. $c;
$a = $b .$c;
$a = $b . $c;
5 changes: 5 additions & 0 deletions Yii2/Tests/Files/SpacesAroundConcatUnitTest.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$a = $b . $c;
$a = $b . $c;
$a = $b . $c;
$a = $b . $c;
44 changes: 44 additions & 0 deletions Yii2/Tests/Files/SpacesAroundConcatUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Unit test class for the SpacesAroundConcat sniff.
*
* @author Marko Kruljac <[email protected]>
*/

namespace Yii2\Tests\Files;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class SpacesAroundConcatUnitTest extends AbstractSniffUnitTest
{
/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array<int, int>
*/
public function getErrorList()
{
return [
2 => 2,
3 => 1,
4 => 1,
];
}

/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array(int => int)
*/
public function getWarningList()
{
return [];
}
}
12 changes: 12 additions & 0 deletions Yii2/Tests/Properties/PrivatePropertiesUnderscoreUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace foo;

use yii\base\Object;

class Bar extends Object
{
public $varA;
protected $varB;
private $varC;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace foo;

use yii\base\Object;

class Bar extends Object
{
public $varA;
protected $varB;
private $_varC;
}
42 changes: 42 additions & 0 deletions Yii2/Tests/Properties/PrivatePropertiesUnderscoreUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Unit test class for the PrivatePropertiesUnderscore sniff.
*
* @author Marko Kruljac <[email protected]>
*/

namespace Yii2\Tests\Properties;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class PrivatePropertiesUnderscoreUnitTest extends AbstractSniffUnitTest
{
/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array<int, int>
*/
public function getErrorList()
{
return [
11 => 1,
];
}

/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array(int => int)
*/
public function getWarningList()
{
return [];
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@
},
"require": {
"php": ">=5.4.0",
"squizlabs/php_codesniffer": ">=2.3.1 <3.0"
"squizlabs/php_codesniffer": ">=3.0"
}
}