getDependencies() as $dependency) { $this->validateEdge($task, $dependency, $constraint); } } private function validateEdge(Task $task, TaskDependency $dependency, Constraint $constraint): void { $dependsOn = $dependency->getDependsOn(); if (null === $dependsOn) { return; } if ($dependsOn === $task || $dependsOn->getId() === $task->getId()) { $this->context->buildViolation($constraint::SELF_REFERENCE)->atPath('dependencies')->addViolation(); return; } if (null !== $task->getInitiative() && null !== $dependsOn->getInitiative() && $task->getInitiative()->getId() !== $dependsOn->getInitiative()->getId()) { $this->context->buildViolation($constraint::OTHER_PLAN)->atPath('dependencies')->addViolation(); return; } // Cycle check: walk the edges from the depends-on task; if we come back to this task, it's a cycle. $seen = []; $queue = [$dependsOn]; while (null !== ($current = array_shift($queue))) { $key = $current->getId() ?? spl_object_id($current); if ($key === ($task->getId() ?? spl_object_id($task))) { $this->context->buildViolation($constraint::CYCLE)->atPath('dependencies')->addViolation(); return; } if (isset($seen[$key])) { continue; } $seen[$key] = true; // Follow the edges of the current task, from the database (they may belong to other tasks). $edges = $this->em->getRepository(TaskDependency::class)->findBy(['task' => $current]); foreach ($edges as $edge) { if (null !== $edge->getDependsOn()) { $queue[] = $edge->getDependsOn(); } } } } }