From 546c1ff4c2550fd06ee2e9a8d116fd5332e858be Mon Sep 17 00:00:00 2001 From: Tom Oram Date: Mon, 30 Dec 2013 16:40:58 +0000 Subject: [PATCH] Refactor serveral duplicate loops into a method --- .../TokenReflection/StaticCodeAnalysis.php | 85 +++++++------------ 1 file changed, 33 insertions(+), 52 deletions(-) diff --git a/src/main/QafooLabs/Refactoring/Adapters/TokenReflection/StaticCodeAnalysis.php b/src/main/QafooLabs/Refactoring/Adapters/TokenReflection/StaticCodeAnalysis.php index 0d66146..f43583a 100644 --- a/src/main/QafooLabs/Refactoring/Adapters/TokenReflection/StaticCodeAnalysis.php +++ b/src/main/QafooLabs/Refactoring/Adapters/TokenReflection/StaticCodeAnalysis.php @@ -34,59 +34,31 @@ public function __construct() public function isMethodStatic(File $file, LineRange $range) { - $this->broker = new Broker(new Memory); - $file = $this->broker->processString($file->getCode(), $file->getRelativePath(), true); - $lastLine = $range->getEnd(); - - foreach ($file->getNamespaces() as $namespace) { - foreach ($namespace->getClasses() as $class) { - foreach ($class->getMethods() as $method) { - if ($method->getStartLine() < $lastLine && $lastLine < $method->getEndLine()) { - return $method->isStatic(); - } - } - } - } + $method = $this->findMatchingMethod($file, $range); - return false; + return $method ? $method->isStatic() : false; } public function getMethodEndLine(File $file, LineRange $range) { - $this->broker = new Broker(new Memory); - $file = $this->broker->processString($file->getCode(), $file->getRelativePath(), true); - $lastLine = $range->getEnd(); + $method = $this->findMatchingMethod($file, $range); - foreach ($file->getNamespaces() as $namespace) { - foreach ($namespace->getClasses() as $class) { - foreach ($class->getMethods() as $method) { - if ($method->getStartLine() < $lastLine && $lastLine < $method->getEndLine()) { - return $method->getEndLine(); - } - } - } + if ($method === null) { + throw new \InvalidArgumentException("Could not find method end line."); } - throw new \InvalidArgumentException("Could not find method end line."); + return $method->getEndLine(); } public function getMethodStartLine(File $file, LineRange $range) { - $this->broker = new Broker(new Memory); - $file = $this->broker->processString($file->getCode(), $file->getRelativePath(), true); - $lastLine = $range->getEnd(); + $method = $this->findMatchingMethod($file, $range); - foreach ($file->getNamespaces() as $namespace) { - foreach ($namespace->getClasses() as $class) { - foreach ($class->getMethods() as $method) { - if ($method->getStartLine() < $lastLine && $lastLine < $method->getEndLine()) { - return $method->getStartLine(); - } - } - } + if ($method === null) { + throw new \InvalidArgumentException("Could not find method start line."); } - throw new \InvalidArgumentException("Could not find method start line."); + return $method->getStartLine(); } public function getLineOfLastPropertyDefinedInScope(File $file, $lastLine) @@ -115,20 +87,7 @@ public function getLineOfLastPropertyDefinedInScope(File $file, $lastLine) public function isInsideMethod(File $file, LineRange $range) { - $this->broker = new Broker(new Memory); - $file = $this->broker->processString($file->getCode(), $file->getRelativePath(), true); - - foreach ($file->getNamespaces() as $namespace) { - foreach ($namespace->getClasses() as $class) { - foreach ($class->getMethods() as $method) { - if ($method->getStartLine() < $range->getStart() && $range->getEnd() < $method->getEndLine()) { - return true; - } - } - } - } - - return false; + return $this->findMatchingMethod($file, $range) !== null; } /** @@ -154,4 +113,26 @@ public function findClasses(File $file) return $classes; } + + private function findMatchingMethod(File $file, LineRange $range) + { + $foundMethod = null; + + $this->broker = new Broker(new Memory); + $file = $this->broker->processString($file->getCode(), $file->getRelativePath(), true); + $lastLine = $range->getEnd(); + + foreach ($file->getNamespaces() as $namespace) { + foreach ($namespace->getClasses() as $class) { + foreach ($class->getMethods() as $method) { + if ($method->getStartLine() < $lastLine && $lastLine < $method->getEndLine()) { + $foundMethod = $method; + break; + } + } + } + } + + return $foundMethod; + } }