Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
coder
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
coder
Commits
f4ae9081
Commit
f4ae9081
authored
8 years ago
by
Klaus Purer
Browse files
Options
Downloads
Patches
Plain Diff
fix(ClassCreateInstanceSniff): Fix false positive for PHP 7 return type hint (
#2855714
)
parent
58b65c0d
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php
+6
-2
6 additions, 2 deletions
...niffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php
coder_sniffer/Drupal/Test/good/good.php
+21
-0
21 additions, 0 deletions
coder_sniffer/Drupal/Test/good/good.php
with
27 additions
and
2 deletions
coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php
+
6
−
2
View file @
f4ae9081
...
@@ -49,8 +49,12 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_
...
@@ -49,8 +49,12 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_
// next semicolon.
// next semicolon.
$nextParenthesis
=
$phpcsFile
->
findNext
(
T_OPEN_PARENTHESIS
,
$stackPtr
,
null
,
false
,
null
,
true
);
$nextParenthesis
=
$phpcsFile
->
findNext
(
T_OPEN_PARENTHESIS
,
$stackPtr
,
null
,
false
,
null
,
true
);
// If there is a parenthesis owner then this is not a constructor call,
// If there is a parenthesis owner then this is not a constructor call,
// but rather some array or somehting else.
// but rather some array or somehting else. There seems to be a bug in PHPCS
if
(
$nextParenthesis
===
false
||
isset
(
$tokens
[
$nextParenthesis
][
'parenthesis_owner'
])
===
true
)
{
// that finds PHP 7 array return type hints as parenthesis owner, exclude
// that.
if
(
$nextParenthesis
===
false
||
(
isset
(
$tokens
[
$nextParenthesis
][
'parenthesis_owner'
])
===
true
&&
$tokens
[
$tokens
[
$nextParenthesis
][
'parenthesis_owner'
]][
'code'
]
!==
T_RETURN_TYPE
)
)
{
$error
=
'Calling class constructors must always include parentheses'
;
$error
=
'Calling class constructors must always include parentheses'
;
$constructor
=
$phpcsFile
->
findNext
(
PHP_CodeSniffer_Tokens
::
$emptyTokens
,
(
$stackPtr
+
1
),
null
,
true
,
null
,
true
);
$constructor
=
$phpcsFile
->
findNext
(
PHP_CodeSniffer_Tokens
::
$emptyTokens
,
(
$stackPtr
+
1
),
null
,
true
,
null
,
true
);
// We can invoke the fixer if we know this is a static constructor
// We can invoke the fixer if we know this is a static constructor
...
...
This diff is collapsed.
Click to expand it.
coder_sniffer/Drupal/Test/good/good.php
+
21
−
0
View file @
f4ae9081
...
@@ -1547,3 +1547,24 @@ function test22(MyInterface $a) {
...
@@ -1547,3 +1547,24 @@ function test22(MyInterface $a) {
function
test23
():
TestReturnType
{
function
test23
():
TestReturnType
{
return
foo
();
return
foo
();
}
}
/**
* Test class.
*/
class
Test2
{
/**
* Using PHP 7 return type hints is fine.
*
* @return ValidatorInterface[]
* The validators.
*/
public
function
getValidators
():
array
{
return
[
new
PublishedNodesValidator
(),
new
MinimumNodesValidator
(
$this
->
nrOfArticles
),
new
AccessibleOnCurrentDomainValidator
(
$this
->
sectionService
),
];
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment