nodeNameResolver = $nodeNameResolver; $this->valueResolver = $valueResolver; $this->phpVersionProvider = $phpVersionProvider; $this->phpVersionFactory = $phpVersionFactory; } public function resolveFromExpr(Expr $expr): ?ConditionInterface { if ($this->isVersionCompareFuncCall($expr)) { /** @var FuncCall $expr */ return $this->resolveVersionCompareConditionForFuncCall($expr); } if (! $expr instanceof Identical && ! $expr instanceof Equal && ! $expr instanceof NotIdentical && ! $expr instanceof NotEqual) { return null; } $binaryClass = get_class($expr); if ($this->isVersionCompareFuncCall($expr->left)) { /** @var FuncCall $funcCall */ $funcCall = $expr->left; return $this->resolveFuncCall($funcCall, $expr->right, $binaryClass); } if ($this->isVersionCompareFuncCall($expr->right)) { /** @var FuncCall $funcCall */ $funcCall = $expr->right; $versionCompareCondition = $this->resolveVersionCompareConditionForFuncCall($funcCall); if (! $versionCompareCondition instanceof VersionCompareCondition) { return null; } $expectedValue = $this->valueResolver->getValue($expr->left); return new BinaryToVersionCompareCondition($versionCompareCondition, $binaryClass, $expectedValue); } return null; } private function isVersionCompareFuncCall(Node $node): bool { if (! $node instanceof FuncCall) { return false; } return $this->nodeNameResolver->isName($node, 'version_compare'); } private function resolveVersionCompareConditionForFuncCall(FuncCall $funcCall): ?VersionCompareCondition { $firstVersion = $this->resolveArgumentValue($funcCall, 0); if ($firstVersion === null) { return null; } $secondVersion = $this->resolveArgumentValue($funcCall, 1); if ($secondVersion === null) { return null; } // includes compare sign as 3rd argument $versionCompareSign = null; if (isset($funcCall->args[2])) { $versionCompareSign = $this->valueResolver->getValue($funcCall->args[2]->value); } return new VersionCompareCondition($firstVersion, $secondVersion, $versionCompareSign); } private function resolveFuncCall( FuncCall $funcCall, Expr $expr, string $binaryClass ): ?BinaryToVersionCompareCondition { $versionCompareCondition = $this->resolveVersionCompareConditionForFuncCall($funcCall); if (! $versionCompareCondition instanceof VersionCompareCondition) { return null; } $expectedValue = $this->valueResolver->getValue($expr); return new BinaryToVersionCompareCondition($versionCompareCondition, $binaryClass, $expectedValue); } private function resolveArgumentValue(FuncCall $funcCall, int $argumentPosition): ?int { $firstArgValue = $funcCall->args[$argumentPosition]->value; /** @var mixed|null $version */ $version = $this->valueResolver->getValue($firstArgValue); if (in_array($version, ['PHP_VERSION', 'PHP_VERSION_ID'], true)) { return $this->phpVersionProvider->provide(); } if (is_string($version)) { return $this->phpVersionFactory->createIntVersion($version); } return $version; } }