Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Commit

Permalink
Refactor serveral duplicate loops into a method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Oram committed Dec 30, 2013
1 parent c893442 commit 546c1ff
Showing 1 changed file with 33 additions and 52 deletions.
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}
}

0 comments on commit 546c1ff

Please sign in to comment.