Skip to content

ThrowsScope: better naming #41

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 1 commit into from
Nov 27, 2018
Merged
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
44 changes: 22 additions & 22 deletions src/Rules/ThrowsScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,58 @@ class ThrowsScope
private const CAUGHT_EXCEPTIONS_ATTRIBUTE = '__CAUGHT_EXCEPTIONS_ATTRIBUTE__';

/**
* @var array<Type|null>
* @var int
*/
private $throwsAnnotationBlock = [];
private $stackIndex = -1;

/**
* @var int
* @var array<Type|null>
*/
private $throwsAnnotationBlockIndex = -1;
private $throwsAnnotationBlockStack = [];

/**
* @var bool[][]
*/
private $usedThrowsAnnotations = [];
private $usedThrowsAnnotationsStack = [];

/**
* @var TryCatch[][]
*/
private $tryCatchQueue = [];
private $tryCatchStack = [];

public function enterToThrowsAnnotationBlock(?Type $type): void
{
$this->throwsAnnotationBlockIndex++;
$this->stackIndex++;

$this->throwsAnnotationBlock[$this->throwsAnnotationBlockIndex] = $type;
$this->usedThrowsAnnotations[$this->throwsAnnotationBlockIndex] = [];
$this->tryCatchQueue[$this->throwsAnnotationBlockIndex] = [];
$this->throwsAnnotationBlockStack[$this->stackIndex] = $type;
$this->usedThrowsAnnotationsStack[$this->stackIndex] = [];
$this->tryCatchStack[$this->stackIndex] = [];
}

/**
* @return string[]
*/
public function exitFromThrowsAnnotationBlock(): array
{
$usedThrowsAnnotations = $this->usedThrowsAnnotations[$this->throwsAnnotationBlockIndex];
$usedThrowsAnnotations = $this->usedThrowsAnnotationsStack[$this->stackIndex];

unset($this->throwsAnnotationBlock[$this->throwsAnnotationBlockIndex]);
unset($this->usedThrowsAnnotations[$this->throwsAnnotationBlockIndex]);
unset($this->tryCatchQueue[$this->throwsAnnotationBlockIndex]);
unset($this->throwsAnnotationBlockStack[$this->stackIndex]);
unset($this->usedThrowsAnnotationsStack[$this->stackIndex]);
unset($this->tryCatchStack[$this->stackIndex]);

$this->throwsAnnotationBlockIndex--;
$this->stackIndex--;

return array_keys($usedThrowsAnnotations);
}

public function enterToTryCatch(TryCatch $tryCatch): void
{
$this->tryCatchQueue[$this->throwsAnnotationBlockIndex][] = $tryCatch;
$this->tryCatchStack[$this->stackIndex][] = $tryCatch;
}

public function exitFromTry(): void
{
array_pop($this->tryCatchQueue[$this->throwsAnnotationBlockIndex]);
array_pop($this->tryCatchStack[$this->stackIndex]);
}

/**
Expand All @@ -93,8 +93,8 @@ public function getCaughtExceptions(Name $name): array

private function isExceptionCaught(string $exceptionClassName): bool
{
foreach (array_reverse(array_keys($this->tryCatchQueue[$this->throwsAnnotationBlockIndex])) as $catchKey) {
$catches = $this->tryCatchQueue[$this->throwsAnnotationBlockIndex][$catchKey];
foreach (array_reverse(array_keys($this->tryCatchStack[$this->stackIndex])) as $catchKey) {
$catches = $this->tryCatchStack[$this->stackIndex][$catchKey];

foreach ($catches->catches as $catch) {
foreach ($catch->types as $type) {
Expand All @@ -116,11 +116,11 @@ private function isExceptionCaught(string $exceptionClassName): bool
}
}

if ($this->throwsAnnotationBlock[$this->throwsAnnotationBlockIndex] !== null) {
$throwsExceptionClasses = TypeUtils::getDirectClassNames($this->throwsAnnotationBlock[$this->throwsAnnotationBlockIndex]);
if ($this->throwsAnnotationBlockStack[$this->stackIndex] !== null) {
$throwsExceptionClasses = TypeUtils::getDirectClassNames($this->throwsAnnotationBlockStack[$this->stackIndex]);
foreach ($throwsExceptionClasses as $throwsExceptionClass) {
if (is_a($exceptionClassName, $throwsExceptionClass, true)) {
$this->usedThrowsAnnotations[$this->throwsAnnotationBlockIndex][$throwsExceptionClass] = true;
$this->usedThrowsAnnotationsStack[$this->stackIndex][$throwsExceptionClass] = true;
return true;
}
}
Expand Down