Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 9 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ jobs:
- name: Warm Up TypePHP Cache
run: php bin/typephp cache:warm

- name: Run Test Suite with Coverage (Pest)
- name: Run Runtime Type Checking Engine Suite (Pest)
run: ./vendor/bin/pest tests/TypeChecking --compact

- name: Run Full Test Suite with Coverage (Pest)
run: ./vendor/bin/pest --coverage-clover=clover.xml --compact
if: matrix.os == 'ubuntu-latest' && matrix.php == '8.4'

- name: Run Full Test Suite (Pest)
run: ./vendor/bin/pest --compact
if: "! (matrix.os == 'ubuntu-latest' && matrix.php == '8.4')"

- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v5
if: matrix.os == 'ubuntu-latest' && matrix.php == '8.4'
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: clover.xml
fail_ci_if_error: false

- name: Run Test Suite (Pest)
run: ./vendor/bin/pest --compact
if: "! (matrix.os == 'ubuntu-latest' && matrix.php == '8.4')"
fail_ci_if_error: false
10 changes: 2 additions & 8 deletions src/Contract/ContractParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ private static function parseFunction(\ReflectionFunction $ref): array
$stubDoc = StubManager::getFunctionDoc($funcName);
$doc = $stubDoc ?? $ref->getDocComment();

if ($doc === false || $doc === null) {
if ($doc === false || $doc === null || self::shouldIgnoreDoc($doc)) {
return [
'types' => [],
'templates' => [],
Expand Down Expand Up @@ -563,12 +563,10 @@ private static function parseFunction(\ReflectionFunction $ref): array
}

$pObj = $baseParamObjects[$paramName] ?? null;
$isTemplateType = ($type instanceof IdentifierTypeNode && isset($templates[$type->name]));
if (
Config::isRespectNativeNullabilityEnabled()
&& $pObj !== null
&& self::parameterExplicitlyAllowsNull($pObj)
&& ! $isTemplateType
&& ! self::typeContainsNull($type)
) {
$type = new NullableTypeNode($type);
Expand Down Expand Up @@ -692,7 +690,7 @@ private static function parseMethodHierarchyDocs(
}

$doc = $stubDoc ?? $hierRef->getDocComment();
if ($doc === false || $doc === null) {
if ($doc === false || $doc === null || self::shouldIgnoreDoc($doc)) {
continue;
}

Expand Down Expand Up @@ -734,12 +732,10 @@ private static function parseMethodHierarchyDocs(
}

$pObj = $baseParamObjects[$targetParamName] ?? null;
$isTemplateType = ($type instanceof IdentifierTypeNode && isset($templates[$type->name]));
if (
Config::isRespectNativeNullabilityEnabled()
&& $pObj !== null
&& self::parameterExplicitlyAllowsNull($pObj)
&& ! $isTemplateType
&& ! self::typeContainsNull($type)
) {
$type = new NullableTypeNode($type);
Expand Down Expand Up @@ -857,11 +853,9 @@ private static function applyConstructorPromotionFallback(
$resolvedProp = new ArrayTypeNode($resolvedProp);
}

$isTemplateType = ($resolvedProp instanceof IdentifierTypeNode && isset($classTemplates[$resolvedProp->name]));
if (
Config::isRespectNativeNullabilityEnabled()
&& self::parameterExplicitlyAllowsNull($p)
&& ! $isTemplateType
&& ! self::typeContainsNull($resolvedProp)
) {
$resolvedProp = new NullableTypeNode($resolvedProp);
Expand Down
46 changes: 44 additions & 2 deletions src/Internal/Checker/ParamChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,7 @@ private static function validateSingleParam(

$isClassStringT = ($typeNode instanceof GenericTypeNode && self::isClassStringTemplate($typeNode, $templates));

$isBareTemplate = ($typeNode instanceof IdentifierTypeNode && isset($templates[$typeNode->name]))
|| ($typeNode instanceof ArrayTypeNode && $typeNode->type instanceof IdentifierTypeNode && isset($templates[$typeNode->type->name]));
$isBareTemplate = self::getTemplateName($typeNode, $templates) !== null;

$shouldSkipTemplateSub = $isBareTemplate || $isClassStringT;

Expand Down Expand Up @@ -557,10 +556,27 @@ private static function getTemplateName(TypeNode $typeNode, array $templates): ?
return $typeNode->name;
}

if ($typeNode instanceof NullableTypeNode && $typeNode->type instanceof IdentifierTypeNode && isset($templates[$typeNode->type->name])) {
return $typeNode->type->name;
}

if ($typeNode instanceof ArrayTypeNode && $typeNode->type instanceof IdentifierTypeNode && isset($templates[$typeNode->type->name])) {
return $typeNode->type->name;
}

if ($typeNode instanceof UnionTypeNode && \count($typeNode->types) === 2) {
$t0 = $typeNode->types[0];
$t1 = $typeNode->types[1];

if ($t0 instanceof IdentifierTypeNode && isset($templates[$t0->name]) && $t1 instanceof IdentifierTypeNode && strtolower($t1->name) === 'null') {
return $t0->name;
}

if ($t1 instanceof IdentifierTypeNode && isset($templates[$t1->name]) && $t0 instanceof IdentifierTypeNode && strtolower($t0->name) === 'null') {
return $t1->name;
}
}

return null;
}

Expand All @@ -583,6 +599,11 @@ private static function resolveTemplateParam(

$templateNode = $templates[$templateName];
$isVariadic = $typeNode instanceof ArrayTypeNode;
$isNullable = ($typeNode instanceof NullableTypeNode) || ($typeNode instanceof UnionTypeNode && self::typeContainsNull($typeNode));

if ($isNullable && $val === null) {
return null;
}

$contract = ContractParser::parse($function);
$classTemplates = $contract['classTemplates'] ?? [];
Expand Down Expand Up @@ -641,4 +662,25 @@ private static function resolveTemplateParam(

return null;
}

private static function typeContainsNull(TypeNode $node): bool
{
if ($node instanceof NullableTypeNode) {
return true;
}

if ($node instanceof IdentifierTypeNode && strtolower($node->name) === 'null') {
return true;
}

if ($node instanceof UnionTypeNode) {
foreach ($node->types as $t) {
if (self::typeContainsNull($t)) {
return true;
}
}
}

return false;
}
}
14 changes: 2 additions & 12 deletions src/Internal/ContractVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace TypePHP\Internal;

use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use TypePHP\Contract\DocblockExtractor;
use TypePHP\Internal\Visitor\FunctionContractInjector;
Expand All @@ -28,9 +27,9 @@ public function __construct()
/**
* Traverses and transforms AST nodes during entry.
*
* @return array<Node>|int|null
* @return array<Node>|null
*/
public function enterNode(Node $node): array|int|null
public function enterNode(Node $node): ?array
{
if ($node instanceof Node\Stmt\Function_
|| $node instanceof Node\Stmt\ClassMethod
Expand All @@ -49,15 +48,6 @@ public function enterNode(Node $node): array|int|null
}

if ($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassMethod) {
$doc = $node->getDocComment();
if ($doc !== null) {
$docText = $doc->getText();
$shouldRespectIgnore = (bool) (Config::get()['respect_ignore_tags'] ?? true);
if ($shouldRespectIgnore && (str_contains($docText, '@typephp-ignore') || str_contains($docText, '@typephp-disable'))) {
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}
}

FunctionContractInjector::inject($node);

return null;
Expand Down
12 changes: 0 additions & 12 deletions src/Internal/Visitor/FunctionContractInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use TypePHP\Internal\Config;

/**
* @internal Injects parameter checks, return checks, and generator interceptors into functions and methods.
Expand All @@ -29,10 +28,6 @@ public static function inject(Node\Stmt\Function_|Node\Stmt\ClassMethod $node):

$docText = $doc !== null ? $doc->getText() : '';

if (self::shouldSkipInjection($docText)) {
return;
}

$methodName = $isClassMethod ? strtolower($node->name->toString()) : '';
$isMagicLifecycle = $isClassMethod && \in_array($methodName, ['__construct', '__destruct', '__clone'], true);

Expand Down Expand Up @@ -123,13 +118,6 @@ private static function hasReturnContracts(string $docText, bool $isClassMethod)
return false;
}

private static function shouldSkipInjection(string $docText): bool
{
$shouldRespectIgnore = (bool) (Config::get()['respect_ignore_tags'] ?? true);

return $shouldRespectIgnore && (str_contains($docText, '@typephp-ignore') || str_contains($docText, '@typephp-disable'));
}

private static function resolveThisArg(bool $isClassMethod, Node\Stmt\Function_|Node\Stmt\ClassMethod $node): Node\Expr
{
if (! $isClassMethod) {
Expand Down
5 changes: 5 additions & 0 deletions src/Resolver/SpecialTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ private static function getReflectionContext(\ReflectionClass|\ReflectionFunctio
return self::$reflectionContextCache[$context] = new \ReflectionClass($fallbackClass);
}

if (class_exists($context) || interface_exists($context) || trait_exists($context) || enum_exists($context)) {
/** @var class-string<object> $context */
return self::$reflectionContextCache[$context] = new \ReflectionClass($context);
}

try {
return self::$reflectionContextCache[$context] = new \ReflectionFunction($context);
} catch (\ReflectionException $e) {
Expand Down
Loading
Loading