From 1552b4bf74f9e87228bfeeb6e32e4ce5cb3d7de3 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Sat, 27 Jan 2018 15:44:32 -0500 Subject: [PATCH] Convert validUnusedVariableNames to array late Previously the property was converted to an array when the Sniff was registered. This could cause issues if the property was changed (I know it's unlikely but it can happen in tests pretty easily) after registration. Furthermore, it was convered to an array and then used to replace the original string property, which introduces risk by both mutating a property during an object lifecycle and even changing its type. With this change, we never change the property from a string, and instead we convert it to an array just before it is used. --- .../Sniffs/CodeAnalysis/VariableAnalysisSniff.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index 8f1fb270..896a2f9c 100644 --- a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php +++ b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php @@ -48,10 +48,6 @@ class VariableAnalysisSniff implements Sniff { public $validUnusedVariableNames = null; public function register() { - if (!empty($this->validUnusedVariableNames)) { - $this->validUnusedVariableNames = - preg_split('/\s+/', trim($this->validUnusedVariableNames)); - } return [ T_VARIABLE, T_DOUBLE_QUOTED_STRING, @@ -124,7 +120,10 @@ protected function getOrCreateVariableInfo($varName, $currScope) { $scopeInfo = $this->getOrCreateScopeInfo($currScope); if (!isset($scopeInfo->variables[$varName])) { $scopeInfo->variables[$varName] = new VariableInfo($varName); - if ($this->validUnusedVariableNames && in_array($varName, $this->validUnusedVariableNames)) { + $validUnusedVariableNames = (empty($this->validUnusedVariableNames)) + ? [] + : preg_split('/\s+/', trim($this->validUnusedVariableNames)); + if (in_array($varName, $validUnusedVariableNames)) { $scopeInfo->variables[$varName]->ignoreUnused = true; } }