Skip to content
Open
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
4 changes: 1 addition & 3 deletions src/Base/Transformer/TsAssocArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public static function canTransform(Type $type, Scope $scope, ReflectionProvider
return false;
}

$keyType = $type->getKeyType();

return ! $keyType instanceof \PHPStan\Type\IntegerType;
return $type->getKeyType()->isInteger()->no();
}

public static function transform(Type $type, Scope $scope, ReflectionProvider $reflectionProvider): TsType {
Expand Down
5 changes: 2 additions & 3 deletions src/Base/Transformer/TsSimpleArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\StringType;

/**
* A simple homogeneous array type. For example: `string[]`, `number[]`, `(string | number)[]`, `never[]`, etc.
Expand All @@ -20,9 +21,7 @@ public static function canTransform(Type $type, Scope $scope, ReflectionProvider
return false;
}

$keyType = $type->getKeyType();

return $keyType instanceof \PHPStan\Type\IntegerType;
return !$type->getKeyType()->isInteger()->no();
}

public static function transform(Type $type, Scope $scope, ReflectionProvider $reflectionProvider): TsSimpleArrayType {
Expand Down
2 changes: 1 addition & 1 deletion src/Base/Transformer/_ArrayLikeParserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class _ArrayLikeParserHelper
{
public static function transform(Type $keyType, Type $valueType, Scope $scope, ReflectionProvider $reflectionProvider): TsSimpleArrayType|TsRecordType
{
if ($keyType->isInteger()->yes()) {
if (!$keyType->isInteger()->no()) {
return new TsSimpleArrayType(TsTransformer::transform($valueType, $scope, $reflectionProvider));
}

Expand Down
26 changes: 11 additions & 15 deletions src/LaravelData/Transformer/LaravelDataPaginatedTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public static function canTransform(Type $type, Scope $scope, ReflectionProvider

$unionTypes = $type->getTypes();

if (count($unionTypes) !== 2) {
return false;
}
// if (count($unionTypes) !== 2) {
// return false;
// }

foreach ($unionTypes as $unionType) {
if (!$unionType instanceof \PHPStan\Type\ObjectType) {
if (!$unionType instanceof \PHPStan\Type\ObjectType && !$unionType instanceof \PHPStan\Type\ArrayType) {
return false;
}
}
Expand All @@ -40,10 +40,14 @@ public static function canTransform(Type $type, Scope $scope, ReflectionProvider
}

/**
* @param \PHPStan\Type\ObjectType[] $types
* @param (\PHPStan\Type\ObjectType | \PHPStan\Type\ArrayType)[] $types
*/
protected static function getPaginator(array $types, ReflectionProvider $reflectionProvider): ?Type {
$paginator = array_filter($types, function ($type) use ($reflectionProvider) {
if (!$type instanceof \PHPStan\Type\ObjectType) {
return false;
}

if ($type->getClassName() === 'Illuminate\Pagination\AbstractPaginator') {
return true;
}
Expand All @@ -57,11 +61,7 @@ protected static function getPaginator(array $types, ReflectionProvider $reflect
return $reflection->isSubclassOfClass($reflectionProvider->getClass('Illuminate\Pagination\AbstractPaginator'));
});

if (count($paginator) === 1) {
return array_pop($paginator);
}

return null;
return array_pop($paginator);
}

/**
Expand All @@ -74,11 +74,7 @@ protected static function getPaginatedType(array $types): ?Type {
return $type->isIterable()->yes();
});

if (count($types) !== 1) {
return null;
}

return array_pop($types)->getIterableValueType();
return array_pop($types)?->getIterableValueType();
}

public static function transform(Type $type, Scope $scope, ReflectionProvider $reflectionProvider): TsType {
Expand Down
21 changes: 20 additions & 1 deletion src/LaravelData/Transformer/LaravelDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use djfhe\PHPStanTypescriptTransformer\TsType;
use djfhe\PHPStanTypescriptTransformer\Base\Types\TsObjectPropertyType;
use djfhe\PHPStanTypescriptTransformer\Base\Types\TsObjectType;
use djfhe\PHPStanTypescriptTransformer\Base\Types\TsLiteralType;
use djfhe\PHPStanTypescriptTransformer\TsTransformer;
use PHPStan\Analyser\OutOfClassScope;
use ReflectionProperty;
Expand Down Expand Up @@ -147,7 +148,25 @@ protected static function parseLiteralTypescriptAttribute(array $attributes, Sco

assert($arg !== null);

return TsTransformer::transform($arg, $scope, $reflectionProvider);
// $arg is a string or constant string type. if it we would transform it via ::transform,
// we would get a string with the content as its type. E.g.:
// #[LiteralTypeScriptType('{ a: string }')]
// public mixed $foo
// would be transformed to:
// foo: '{ a: string }'
// which is wrong. We need to directly handle the string content here.

if (!$arg->isString()->yes()) {
return null;
}

if ($arg instanceof \PHPStan\Type\Constant\ConstantStringType) {
$arg = $arg->getValue();
} else {
$arg = 'unknown';
}

return new TsLiteralType($arg);
}

public static function transformPriority(Type $type, Scope $scope, ReflectionProvider $reflectionProvider, array $candidates): int
Expand Down