Skip to content
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
16 changes: 11 additions & 5 deletions src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,29 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

$stopPoints = [
T_SEMICOLON,
T_OPEN_CURLY_BRACKET,
T_CLOSE_CURLY_BRACKET,
];

$endOfPreviousStatement = $phpcsFile->findPrevious($stopPoints, ($stackPtr - 1), null, false, null, true);

$validPrefixes = Tokens::$methodPrefixes;
$validPrefixes[] = T_VAR;
$validPrefixes[] = T_READONLY;

$startOfStatement = $phpcsFile->findPrevious($validPrefixes, ($stackPtr - 1), null, false, null, true);
$startOfStatement = $phpcsFile->findNext($validPrefixes, ($endOfPreviousStatement + 1), $stackPtr, false, null, true);
if ($startOfStatement === false) {
// Parse error/live coding - property without modifier. Bow out.
return;
}

$endOfStatement = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1), null, false, null, true);

$ignore = $validPrefixes;
$ignore[T_WHITESPACE] = T_WHITESPACE;

$start = $startOfStatement;
for ($prev = ($startOfStatement - 1); $prev >= 0; $prev--) {
if (isset($ignore[$tokens[$prev]['code']]) === true) {
if ($tokens[$prev]['code'] === T_WHITESPACE) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
class MyClass
{

public $var1 = 'value';


public ?int $var2 = 'value';

public $var3 = 'value';
Expand All @@ -16,6 +16,7 @@ interface MyInterface

public $var1 = 'value';


public ?Folder\ClassName $var2 = 'value';

protected $var3 = 'value';
Expand All @@ -25,10 +26,12 @@ interface MyInterface
class MyClass
{


public string $var1 = 'value';

private string $var2 = 'value';


protected $var3 = 'value';


Expand All @@ -38,14 +41,12 @@ class MyClass

class MyClass
{

public $var1 = 'value';
}//end class


interface MyInterface
{

public $var1 = 'value';
function myFunction();
}//end interface
Expand All @@ -62,6 +63,7 @@ class MyClass
*/
protected $actions = array();


/**
* TRUE if this step should be performed after the asset is created.
*
Expand All @@ -75,7 +77,6 @@ class MyClass

class MyClass
{

/**
* The actions that this wizard step requires.
*
Expand Down Expand Up @@ -103,8 +104,8 @@ class MyClass
* @var array
* @since 4.0.0
*/
protected $actions = array();

protected $actions = array();
/**
* The actions that this wizard step requires.
*
Expand Down Expand Up @@ -147,6 +148,7 @@ class Foo
private $bar;

// here comes the comment

private $caseStudy = null;

}
Expand All @@ -155,13 +157,11 @@ class Foo

class MyClass
{

public $var1 = 'value';


public $var2 = 'value';


public $var3 = 'value';

}//end class
Expand All @@ -173,6 +173,7 @@ class MyClass
{
public $var1 = 'value';


public $var2 = 'value';

public $var3 = 'value';
Expand All @@ -181,6 +182,7 @@ class MyClass

class MyClass
{

public $var1 = 'value';

public $var2 = 'value';
Expand All @@ -194,15 +196,18 @@ class MyClass
class MyClass
{
public $var1 = 'value';

public $var2 = 'value';
public $var3 = 'value';

}//end class

interface MyInterface
{

/* testing */
public $var1 = 'value';

public $var2 = 'value';
public $var3 = 'value';

Expand All @@ -212,9 +217,13 @@ interface MyInterface
// phpcs:set Squiz.WhiteSpace.MemberVarSpacing spacingBeforeFirst 0

class phpcsCommentTest {


// phpcs:disable Standard.Category.Sniff

public $var1 = 'value';


// phpcs:enable Standard.Category.Sniff

public $var1 = 'value';
Expand All @@ -226,10 +235,12 @@ class phpcsCommentTest {

class phpcsCommentTest {


// phpcs:disable Standard.Category.Sniff

public $var1 = 'value';


// phpcs:enable Standard.Category.Sniff

public $var1 = 'value';
Expand All @@ -238,15 +249,15 @@ class phpcsCommentTest {

class MyOtherClass
{

public
$varK = array( 'a', 'b' );

protected static
$varK,
$varL,
$varM;



private
$varO = true,
$varP = array( 'a' => 'a', 'b' => 'b' ),
Expand All @@ -259,9 +270,9 @@ class ClassUsingSimpleTraits
{
use HelloWorld;


/* comment */
public $firstVar = array( 'a', 'b' );

protected $secondVar = true;
}

Expand All @@ -272,8 +283,11 @@ class ClassUsingComplexTraits
A::bigTalk insteadof B;
}



public $firstVar = array( 'a', 'b' );


/* comment */
protected $secondVar = true;
}
Expand Down Expand Up @@ -311,7 +325,6 @@ class CommentedOutCodeAtStartOfClass {
class CommentedOutCodeAtStartOfClassNoBlankLine {

// phpcs:disable Stnd.Cat.Sniff -- For reasons.

/**
* Description.
*
Expand All @@ -322,21 +335,24 @@ class CommentedOutCodeAtStartOfClassNoBlankLine {

class HasAttributes
{

/**
* Short description of the member variable.
*
* @var array
*/

#[ORM\Id]#[ORM\Column("integer")]

private $id;


/**
* Short description of the member variable.
*
* @var array
*/
#[ORM\GeneratedValue]

#[ORM\Column(ORM\Column::T_INTEGER)]
protected $height;

Expand All @@ -346,7 +362,6 @@ class HasAttributes
#[FirstAttribute]
#[SecondAttribute]
protected $propertyDouble;

#[ThirdAttribute]
protected $propertyWithoutSpacing;
}
Expand All @@ -359,12 +374,14 @@ enum SomeEnum
}

class SupportReadonlyProperties {

readonly int $readonlyA;

public readonly string $publicReadonly;

readonly bool $readonlyB;

readonly private bool $readonlyPrivate;
}

class NoPreambleMultilineDeclaration {
public
static
int $prop = 1;
}
Loading