Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.3.x' into 1.4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 7, 2024
2 parents 3f5f724 + 69e422e commit bc1c6fa
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 32 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
coverage: "none"
php-version: "${{ matrix.php-version }}"
ini-file: development
extensions: "mongodb"

- name: "Validate Composer"
run: "composer validate"
Expand Down Expand Up @@ -70,6 +71,7 @@ jobs:
coverage: "none"
php-version: "8.2"
ini-file: development
extensions: "mongodb"

- name: "Validate Composer"
run: "composer validate"
Expand Down Expand Up @@ -115,6 +117,7 @@ jobs:
coverage: "none"
php-version: "${{ matrix.php-version }}"
ini-file: development
extensions: "mongodb"

- name: "Downgrade PHPUnit"
if: matrix.php-version == '7.2' || matrix.php-version == '7.3'
Expand Down Expand Up @@ -155,9 +158,8 @@ jobs:
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
extensions: mbstring
extensions: "mongodb"
ini-file: development
tools: composer:v2

- name: "Downgrade PHPUnit"
if: matrix.php-version == '7.2' || matrix.php-version == '7.3'
Expand Down
18 changes: 7 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,25 @@
},
"require-dev": {
"composer/semver": "^3.3.2",
"doctrine/annotations": "^1.11.0",
"doctrine/collections": "^1.6",
"doctrine/annotations": "^1.11 || ^2.0",
"doctrine/collections": "^1.6 || ^2.1",
"doctrine/common": "^2.7 || ^3.0",
"doctrine/dbal": "^2.13.8 || ^3.3.3",
"doctrine/lexer": "^1.2.1",
"doctrine/mongodb-odm": "^1.3 || ^2.1",
"doctrine/lexer": "^2.0 || ^3.0",
"doctrine/mongodb-odm": "^1.3 || ^2.4.3",
"doctrine/orm": "^2.14.0",
"doctrine/persistence": "^1.3.8 || ^2.2.1",
"doctrine/persistence": "^2.2.1 || ^3.2",
"gedmo/doctrine-extensions": "^3.8",
"nesbot/carbon": "^2.49",
"nikic/php-parser": "^4.13.2",
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpstan/phpstan-phpunit": "^1.3.13",
"phpstan/phpstan-strict-rules": "^1.5.1",
"phpunit/phpunit": "^9.5.10",
"ramsey/uuid-doctrine": "^1.5.0",
"symfony/cache": "^4.4.35"
"ramsey/uuid": "^4.2",
"symfony/cache": "^5.4"
},
"config": {
"platform": {
"ext-mongo": "1.12",
"ext-mongodb": "1.6.16"
},
"sort-packages": true
},
"extra": {
Expand Down
17 changes: 7 additions & 10 deletions src/Type/Doctrine/Descriptors/Ramsey/UuidTypeDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace PHPStan\Type\Doctrine\Descriptors\Ramsey;

use PHPStan\Rules\Doctrine\ORM\FakeTestingUuidType;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType;
use Ramsey\Uuid\Doctrine\UuidBinaryType;
use Ramsey\Uuid\Doctrine\UuidType;
use Ramsey\Uuid\UuidInterface;
use function in_array;
use function sprintf;
Expand All @@ -19,15 +17,13 @@ class UuidTypeDescriptor implements DoctrineTypeDescriptor
{

private const SUPPORTED_UUID_TYPES = [
UuidType::class,
UuidBinaryType::class,
UuidBinaryOrderedTimeType::class,
'Ramsey\Uuid\Doctrine\UuidType',
'Ramsey\Uuid\Doctrine\UuidBinaryType',
'Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType',
FakeTestingUuidType::class,
];

/**
* @phpstan-var class-string<\Doctrine\DBAL\Types\Type>
* @var string
*/
/** @var string */
private $uuidTypeName;

public function __construct(
Expand All @@ -46,6 +42,7 @@ public function __construct(

public function getType(): string
{
/** @var class-string<\Doctrine\DBAL\Types\Type> */
return $this->uuidTypeName;
}

Expand Down
9 changes: 7 additions & 2 deletions src/Type/Doctrine/Query/QueryResultTypeWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use BackedEnum;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\AST;
Expand Down Expand Up @@ -35,6 +34,7 @@
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use function array_key_exists;
use function array_map;
use function assert;
use function class_exists;
Expand All @@ -56,7 +56,7 @@
*
* It extends SqlkWalker because AST\Node::dispatch() accepts SqlWalker only
*
* @phpstan-type QueryComponent array{metadata: ClassMetadata<object>, parent: mixed, relation: ?array{orderBy: array<array-key, string>, indexBy: ?string, fieldName: string, targetEntity: string, sourceEntity: string, isOwningSide: bool, mappedBy: string, type: int}, map: mixed, nestingLevel: int, token: mixed}
* @phpstan-import-type QueryComponent from Parser
*/
class QueryResultTypeWalker extends SqlWalker
{
Expand Down Expand Up @@ -229,6 +229,7 @@ public function walkPathExpression($pathExpr)
$fieldName = $pathExpr->field;
$dqlAlias = $pathExpr->identificationVariable;
$qComp = $this->queryComponents[$dqlAlias];
assert(array_key_exists('metadata', $qComp));
$class = $qComp['metadata'];

assert($fieldName !== null);
Expand Down Expand Up @@ -516,6 +517,7 @@ public function walkFunction($function)
$dqlAlias = $function->pathExpression->identificationVariable;
$assocField = $function->pathExpression->field;
$queryComp = $this->queryComponents[$dqlAlias];
assert(array_key_exists('metadata', $queryComp));
$class = $queryComp['metadata'];
$assoc = $class->associationMappings[$assocField];
$assocClassName = $assoc['targetEntity'];
Expand Down Expand Up @@ -756,9 +758,11 @@ public function walkSelectExpression($selectExpression)
if (is_string($expr)) {
$dqlAlias = $expr;
$queryComp = $this->queryComponents[$dqlAlias];
assert(array_key_exists('metadata', $queryComp));
$class = $queryComp['metadata'];
$resultAlias = $selectExpression->fieldIdentificationVariable ?? $dqlAlias;

assert(array_key_exists('parent', $queryComp));
if ($queryComp['parent'] !== null) {
return '';
}
Expand All @@ -785,6 +789,7 @@ public function walkSelectExpression($selectExpression)

$dqlAlias = $expr->identificationVariable;
$qComp = $this->queryComponents[$dqlAlias];
assert(array_key_exists('metadata', $qComp));
$class = $qComp['metadata'];

[$typeName, $enumType] = $this->getTypeOfField($class, $fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace PHPStan\DoctrineIntegration\ORM\EntityManagerMergeReturn;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping as ORM;
use function PHPStan\Testing\assertType;

class Example
{
/**
* @var EntityManagerInterface
* @var EntityManager
*/
private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use PHPStan\Type\Doctrine\Descriptors\SimpleArrayType;
use PHPStan\Type\Doctrine\Descriptors\StringType;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use Ramsey\Uuid\Doctrine\UuidType;
use const PHP_VERSION_ID;

/**
Expand All @@ -46,8 +45,8 @@ protected function getRule(): Rule
if (!Type::hasType(CustomNumericType::NAME)) {
Type::addType(CustomNumericType::NAME, CustomNumericType::class);
}
if (!Type::hasType(UuidType::NAME)) {
Type::addType(UuidType::NAME, UuidType::class);
if (!Type::hasType(FakeTestingUuidType::NAME)) {
Type::addType(FakeTestingUuidType::NAME, FakeTestingUuidType::class);
}
if (!Type::hasType('carbon')) {
Type::addType('carbon', CarbonType::class);
Expand All @@ -70,7 +69,7 @@ protected function getRule(): Rule
new IntegerType(),
new StringType(),
new SimpleArrayType(),
new UuidTypeDescriptor(UuidType::class),
new UuidTypeDescriptor(FakeTestingUuidType::class),
new ReflectionDescriptor(CarbonImmutableType::class, $this->createBroker()),
new ReflectionDescriptor(CarbonType::class, $this->createBroker()),
new ReflectionDescriptor(CustomType::class, $this->createBroker()),
Expand Down
91 changes: 91 additions & 0 deletions tests/Rules/Doctrine/ORM/FakeTestingUuidType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\GuidType;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Throwable;
use function is_object;
use function is_string;
use function method_exists;

/**
* From https://github.com/ramsey/uuid-doctrine/blob/fafebbe972cdaba9274c286ea8923e2de2579027/src/UuidType.php
* Copyright (c) 2012-2022 Ben Ramsey <[email protected]>
*/
final class FakeTestingUuidType extends GuidType
{

public const NAME = 'uuid';

/**
* {@inheritdoc}
*
* @throws ConversionException
*/
public function convertToPHPValue($value, AbstractPlatform $platform): ?UuidInterface
{
if ($value instanceof UuidInterface) {
return $value;
}

if (!is_string($value) || $value === '') {
return null;
}

try {
$uuid = Uuid::fromString($value);
} catch (Throwable $e) {
throw ConversionException::conversionFailed($value, self::NAME);
}

return $uuid;
}

/**
* {@inheritdoc}
*
* @throws ConversionException
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null || $value === '') {
return null;
}

if (
$value instanceof UuidInterface
|| (
(is_string($value)
|| (is_object($value) && method_exists($value, '__toString')))
&& Uuid::isValid((string) $value)
)
) {
return (string) $value;
}

throw ConversionException::conversionFailed($value, self::NAME);
}

public function getName(): string
{
return self::NAME;
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

/**
* @return string[]
*/
public function getMappedDatabaseTypes(AbstractPlatform $platform): array
{
return [self::NAME];
}

}

0 comments on commit bc1c6fa

Please sign in to comment.