Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1431735
AssignmentInReturnSniff.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
AssignmentInReturnSniff.php
View Options
<?php
/**
* Sniff to suppress the use of:
* Fail: return $a = 0;
* Pass: return $a == 0
*/
namespace
MediaWiki\Sniffs\Usage
;
use
PHP_CodeSniffer\Files\File
;
use
PHP_CodeSniffer\Sniffs\Sniff
;
use
PHP_CodeSniffer\Util\Tokens
;
class
AssignmentInReturnSniff
implements
Sniff
{
/**
* @inheritDoc
*/
public
function
register
():
array
{
return
[
T_RETURN
,
T_YIELD
,
T_YIELD_FROM
,
];
}
/**
* @param File $phpcsFile
* @param int $stackPtr The current token index.
* @return int
*/
public
function
process
(
File
$phpcsFile
,
$stackPtr
)
{
$tokens
=
$phpcsFile
->
getTokens
();
$searchToken
=
Tokens
::
$assignmentTokens
+
[
T_CLOSURE
,
T_FUNCTION
,
T_ANON_CLASS
,
T_SEMICOLON
,
];
$next
=
$phpcsFile
->
findNext
(
$searchToken
,
$stackPtr
+
1
);
while
(
$next
!==
false
)
{
$code
=
$tokens
[
$next
][
'code'
];
if
(
isset
(
$tokens
[
$next
][
'scope_closer'
]
)
)
{
// Skip to the end of the closure/inner function and continue
$next
=
$phpcsFile
->
findNext
(
$searchToken
,
$tokens
[
$next
][
'scope_closer'
]
+
1
);
continue
;
}
if
(
$code
===
T_SEMICOLON
)
{
// End of return statement found
break
;
}
// Check if any assignment operator was used. Allow T_DOUBLE_ARROW as that can
// be used in an array like `return [ 'foo' => 'bar' ]`
if
(
array_key_exists
(
$code
,
Tokens
::
$assignmentTokens
)
&&
$code
!==
T_DOUBLE_ARROW
)
{
$errorPtr
=
$stackPtr
;
// "yield from" could be multiline, get content from more than one token
$content
=
''
;
do
{
$content
.=
$tokens
[
$stackPtr
][
'content'
];
$stackPtr
++;
}
while
(
$tokens
[
$stackPtr
][
'code'
]
===
T_YIELD_FROM
);
// Split by any whitespaces and build better looking content with one space
$contentPieces
=
preg_split
(
'/
\s
+/'
,
$content
);
$phpcsFile
->
addError
(
'Assignment expression not allowed within "%s".'
,
$errorPtr
,
'AssignmentIn'
.
implode
(
''
,
array_map
(
'ucfirst'
,
$contentPieces
)
),
[
implode
(
' '
,
$contentPieces
)
]
);
break
;
}
$next
=
$phpcsFile
->
findNext
(
$searchToken
,
$next
+
1
);
}
// Do not report multiline yield tokens twice
return
$stackPtr
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 21:01 (1 d, 12 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
0b/a9/95fcbefc4c208198a178d871bb83
Default Alt Text
AssignmentInReturnSniff.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment