Skip to content

Commit acfe65c

Browse files
authored
Consider get_defined_vars() to be a read (#92)
* Tests: add tests for get_defined_vars * Tests: add a variable named get_defined_vars * Remove unused import * Add some phpdoc * Mark all vars read at get_defined_vars * Add more guards to checkForPassByReferenceFunctionCall
1 parent 8611e83 commit acfe65c

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace VariableAnalysis\Lib;
44

55
use PHP_CodeSniffer\Files\File;
6-
use VariableAnalysis\Lib\VariableInfo;
76

87
class Helpers {
98
public static function findContainingOpeningSquareBracket(File $phpcsFile, $stackPtr) {
@@ -84,6 +83,12 @@ public static function findPreviousFunctionPtr(File $phpcsFile, $openPtr) {
8483
return $phpcsFile->findPrevious($functionPtrTypes, $openPtr - 1, null, true, null, true);
8584
}
8685

86+
/**
87+
* @param File $phpcsFile
88+
* @param int $stackPtr
89+
*
90+
* @return int|false
91+
*/
8792
public static function findFunctionCall(File $phpcsFile, $stackPtr) {
8893
$tokens = $phpcsFile->getTokens();
8994

@@ -198,6 +203,12 @@ public static function findFunctionPrototype(File $phpcsFile, $stackPtr) {
198203
return false;
199204
}
200205

206+
/**
207+
* @param File $phpcsFile
208+
* @param int $stackPtr
209+
*
210+
* @return int|false
211+
*/
201212
public static function findVariableScope(File $phpcsFile, $stackPtr) {
202213
$tokens = $phpcsFile->getTokens();
203214
$token = $tokens[$stackPtr];

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,51 @@ public function process(File $phpcsFile, $stackPtr) {
123123
if (($token['code'] === T_STRING) && ($token['content'] === 'compact')) {
124124
return $this->processCompact($phpcsFile, $stackPtr);
125125
}
126+
if ($this->isGetDefinedVars($phpcsFile, $stackPtr)) {
127+
Helpers::debug('get_defined_vars is being called');
128+
return $this->markAllVariablesRead($phpcsFile, $stackPtr);
129+
}
126130
if (($token['code'] === T_CLOSE_CURLY_BRACKET) && isset($token['scope_condition'])) {
127131
return $this->processScopeClose($phpcsFile, $token['scope_condition']);
128132
}
129133
}
130134

135+
protected function isGetDefinedVars(File $phpcsFile, $stackPtr) {
136+
$tokens = $phpcsFile->getTokens();
137+
$token = $tokens[$stackPtr];
138+
if (! $token || $token['content'] !== 'get_defined_vars') {
139+
return false;
140+
}
141+
// Make sure this is a function call
142+
$parenPointer = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, $stackPtr + 2);
143+
if (! $parenPointer) {
144+
return false;
145+
}
146+
return true;
147+
}
148+
131149
protected function getScopeKey($currScope) {
132150
if ($currScope === false) {
133151
$currScope = 'file';
134152
}
135153
return ($this->currentFile ? $this->currentFile->getFilename() : 'unknown file') . ':' . $currScope;
136154
}
137155

156+
/**
157+
* @param int $currScope
158+
*
159+
* @return ?ScopeInfo
160+
*/
138161
protected function getScopeInfo($currScope) {
139162
$scopeKey = $this->getScopeKey($currScope);
140163
return isset($this->scopes[$scopeKey]) ? $this->scopes[$scopeKey] : null;
141164
}
142165

166+
/**
167+
* @param int $currScope
168+
*
169+
* @return ScopeInfo
170+
*/
143171
protected function getOrCreateScopeInfo($currScope) {
144172
$scopeKey = $this->getScopeKey($currScope);
145173
if (!isset($this->scopes[$scopeKey])) {
@@ -241,6 +269,13 @@ protected function markVariableDeclaration(
241269
$varInfo->firstDeclared = $stackPtr;
242270
}
243271

272+
/**
273+
* @param string $varName
274+
* @param int $stackPtr
275+
* @param int $currScope
276+
*
277+
* @return void
278+
*/
244279
protected function markVariableRead($varName, $stackPtr, $currScope) {
245280
$varInfo = $this->getOrCreateVariableInfo($varName, $currScope);
246281
if (isset($varInfo->firstRead) && ($varInfo->firstRead <= $stackPtr)) {
@@ -284,6 +319,25 @@ protected function markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $sta
284319
}
285320
}
286321

322+
/**
323+
* @param File $phpcsFile
324+
* @param int $stackPtr
325+
*
326+
* @return void
327+
*/
328+
protected function markAllVariablesRead(File $phpcsFile, $stackPtr) {
329+
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr);
330+
if (! $currScope) {
331+
return false;
332+
}
333+
$scopeInfo = $this->getOrCreateScopeInfo($currScope);
334+
$count = count($scopeInfo->variables);
335+
Helpers::debug("marking all $count variables in scope as read");
336+
foreach ($scopeInfo->variables as $varInfo) {
337+
$this->markVariableRead($varInfo->name, $stackPtr, $scopeInfo->owner);
338+
}
339+
}
340+
287341
protected function checkForFunctionPrototype(File $phpcsFile, $stackPtr, $varName, $currScope) {
288342
$tokens = $phpcsFile->getTokens();
289343
$token = $tokens[$stackPtr];
@@ -723,7 +777,7 @@ protected function checkForPassByReferenceFunctionCall(File $phpcsFile, $stackPt
723777

724778
// Are we pass-by-reference to known pass-by-reference function?
725779
$functionPtr = Helpers::findFunctionCall($phpcsFile, $stackPtr);
726-
if ($functionPtr === false) {
780+
if ($functionPtr === false || ! isset($tokens[$functionPtr])) {
727781
return false;
728782
}
729783

VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,4 +895,18 @@ public function testUnusedForeachVariablesAreIgnoredIfSet() {
895895
];
896896
$this->assertEquals($expectedWarnings, $lines);
897897
}
898+
899+
public function testGetDefinedVarsCountsAsRead() {
900+
$fixtureFile = $this->getFixture('GetDefinedVarsFixture.php');
901+
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
902+
$phpcsFile->process();
903+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
904+
$expectedWarnings = [
905+
6,
906+
18,
907+
22,
908+
29,
909+
];
910+
$this->assertEquals($expectedWarnings, $lines);
911+
}
898912
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
function send_vars_to_method( $object, $data ) {
4+
$some_number = 4;
5+
$some_string = 'hello';
6+
echo $undefined_data; // should be a warning
7+
$new_data = $object->transform_data( $data );
8+
$object->continue_things( get_defined_vars() );
9+
}
10+
11+
function send_vars_to_method_with_global( $object, $data ) {
12+
global $global_data;
13+
$new_data = $object->transform_data( $data );
14+
$object->continue_things( get_defined_vars() );
15+
}
16+
17+
function send_vars_to_method_with_scope_import( $object, $data ) {
18+
$unused_data = 42; // should be a warning
19+
$imported_data = 76;
20+
return array_map( function( $datum ) use ( $object, $imported_data ) {
21+
$new_data = $object->transform_data( $datum );
22+
echo $undefined_data; // should be a warning
23+
$object->continue_things( get_defined_vars() );
24+
}, $data );
25+
}
26+
27+
function send_var_with_var_named_get_defined_vars( $object, $data ) {
28+
$get_defined_vars = 'hi';
29+
$new_data = $object->transform_data( $data ); // should be a warning
30+
$object->continue_things( $get_defined_vars );
31+
}

0 commit comments

Comments
 (0)