Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F7398115
SpaceAfterCastSniff.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
SpaceAfterCastSniff.php
View Options
<?php
/**
* Ensures there is a single space after cast tokens.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace
PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting
;
use
PHP_CodeSniffer\Files\File
;
use
PHP_CodeSniffer\Sniffs\Sniff
;
use
PHP_CodeSniffer\Util\Tokens
;
class
SpaceAfterCastSniff
implements
Sniff
{
/**
* The number of spaces desired after a cast token.
*
* @var integer
*/
public
$spacing
=
1
;
/**
* Allow newlines instead of spaces.
*
* @var boolean
*/
public
$ignoreNewlines
=
false
;
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public
function
register
()
{
return
Tokens
::
$castTokens
;
}
//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public
function
process
(
File
$phpcsFile
,
$stackPtr
)
{
$tokens
=
$phpcsFile
->
getTokens
();
$this
->
spacing
=
(
int
)
$this
->
spacing
;
$pluralizeSpace
=
's'
;
if
(
$this
->
spacing
===
1
)
{
$pluralizeSpace
=
''
;
}
if
(
$tokens
[
$stackPtr
][
'code'
]
===
T_BINARY_CAST
&&
$tokens
[
$stackPtr
][
'content'
]
===
'b'
)
{
// You can't replace a space after this type of binary casting.
return
;
}
$nextNonEmpty
=
$phpcsFile
->
findNext
(
Tokens
::
$emptyTokens
,
(
$stackPtr
+
1
),
null
,
true
);
if
(
$nextNonEmpty
===
false
)
{
return
;
}
if
(
$this
->
ignoreNewlines
===
true
&&
$tokens
[
$stackPtr
][
'line'
]
!==
$tokens
[
$nextNonEmpty
][
'line'
]
)
{
$phpcsFile
->
recordMetric
(
$stackPtr
,
'Spacing after cast statement'
,
'newline'
);
return
;
}
if
(
$this
->
spacing
===
0
&&
$nextNonEmpty
===
(
$stackPtr
+
1
))
{
$phpcsFile
->
recordMetric
(
$stackPtr
,
'Spacing after cast statement'
,
0
);
return
;
}
$nextNonWhitespace
=
$phpcsFile
->
findNext
(
T_WHITESPACE
,
(
$stackPtr
+
1
),
null
,
true
);
if
(
$nextNonEmpty
!==
$nextNonWhitespace
)
{
$error
=
'Expected %s space%s after cast statement; comment found'
;
$data
=
[
$this
->
spacing
,
$pluralizeSpace
,
];
$phpcsFile
->
addError
(
$error
,
$stackPtr
,
'CommentFound'
,
$data
);
if
(
$tokens
[(
$stackPtr
+
1
)][
'code'
]
===
T_WHITESPACE
)
{
$phpcsFile
->
recordMetric
(
$stackPtr
,
'Spacing after cast statement'
,
$tokens
[(
$stackPtr
+
1
)][
'length'
]);
}
else
{
$phpcsFile
->
recordMetric
(
$stackPtr
,
'Spacing after cast statement'
,
0
);
}
return
;
}
$found
=
0
;
if
(
$tokens
[
$stackPtr
][
'line'
]
!==
$tokens
[
$nextNonEmpty
][
'line'
])
{
$found
=
'newline'
;
}
else
if
(
$tokens
[(
$stackPtr
+
1
)][
'code'
]
===
T_WHITESPACE
)
{
$found
=
$tokens
[(
$stackPtr
+
1
)][
'length'
];
}
$phpcsFile
->
recordMetric
(
$stackPtr
,
'Spacing after cast statement'
,
$found
);
if
(
$found
===
$this
->
spacing
)
{
return
;
}
$error
=
'Expected %s space%s after cast statement; %s found'
;
$data
=
[
$this
->
spacing
,
$pluralizeSpace
,
$found
,
];
$errorCode
=
'TooMuchSpace'
;
if
(
$this
->
spacing
!==
0
)
{
if
(
$found
===
0
)
{
$errorCode
=
'NoSpace'
;
}
else
if
(
$found
!==
'newline'
&&
$found
<
$this
->
spacing
)
{
$errorCode
=
'TooLittleSpace'
;
}
}
$fix
=
$phpcsFile
->
addFixableError
(
$error
,
$stackPtr
,
$errorCode
,
$data
);
if
(
$fix
===
true
)
{
$padding
=
str_repeat
(
' '
,
$this
->
spacing
);
if
(
$found
===
0
)
{
$phpcsFile
->
fixer
->
addContent
(
$stackPtr
,
$padding
);
}
else
{
$phpcsFile
->
fixer
->
beginChangeset
();
$start
=
(
$stackPtr
+
1
);
if
(
$this
->
spacing
>
0
)
{
$phpcsFile
->
fixer
->
replaceToken
(
$start
,
$padding
);
++
$start
;
}
for
(
$i
=
$start
;
$i
<
$nextNonWhitespace
;
$i
++)
{
$phpcsFile
->
fixer
->
replaceToken
(
$i
,
''
);
}
$phpcsFile
->
fixer
->
endChangeset
();
}
}
}
//end process()
}
//end class
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Nov 21, 15:03 (29 m, 36 s)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
e1/ed/2528ff86619c92f8d513f584e605
Default Alt Text
SpaceAfterCastSniff.php (4 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment