Skip to content

Fix recognition of variable functions #27

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

Merged
merged 3 commits into from
Feb 8, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,12 @@ protected function checkForStaticMember(File $phpcsFile, $stackPtr, $varName, $c
if (! in_array($tokens[$classNamePtr]['code'], $staticReferences, true)) {
return false;
}
// "When calling static methods, the function call is stronger than the
// static property operator" so look for a function call.
$parenPointer = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, $stackPtr + 2);
if ($parenPointer) {
return false;
}
return true;
}

Expand Down Expand Up @@ -555,7 +561,7 @@ protected function checkForStaticOutsideClass(File $phpcsFile, $stackPtr, $varNa
return true;
}
if ($this->areAnyConditionsAClass($token['conditions'])) {
return true;
return false;
}
}
$phpcsFile->addError(
Expand Down
12 changes: 12 additions & 0 deletions VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,16 @@ public function testTraitAllowsThis() {
$expectedErrors = [];
$this->assertEquals($expectedErrors, $lines);
}

public function testVariableFunctionCallsCountAsUsage() {
$fixtureFile = $this->getFixture('FunctionWithVariableCallFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [18];
$this->assertEquals($expectedWarnings, $lines);
$lines = $this->getErrorLineNumbersFromFile($phpcsFile);
$expectedErrors = [];
$this->assertEquals($expectedErrors, $lines);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

class MyClass {
public function funcUsingSelfCallbackFromAlias($meta, $callback) {
$get_meta_callback = $callback;
return self::$get_meta_callback( $meta );
}

public function funcUsingSelfCallbackFromArgument($meta, $callback) {
return self::$callback( $meta );
}

public function funcUsingStaticCallbackFromArgument($meta, $callback) {
return static::$callback( $meta );
}

public function funcUsingStaticCallbackWithUndefinedVariable($meta) {
return static::$badName( $meta );
}

public function funcUsingPropertyReference($meta, $callback) {
return $this->$callback( $meta );
}

public function funcUsingDirectCallback($meta, $callback) {
return $callback( $meta );
}

public function funcUsingPropertyReferenceDirectly($meta) {
return $meta;
}

public function funcUsingPropertyReferenceWithSelf($meta) {
return self::$$meta;
}

public function funcUsingPropertyReferenceWithThis($meta) {
return $this->$meta;
}

public function funcUsingPropertyReferenceWithStatic($meta) {
return static::$$meta;
}
}