Skip to content

Commit

Permalink
Get rid of bleedingEdge stubs, use a separate config option for enabl…
Browse files Browse the repository at this point in the history
…ing literal-string parameters
  • Loading branch information
ondrejmirtes committed Aug 5, 2024
1 parent ca69937 commit e71f08d
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 319 deletions.
9 changes: 7 additions & 2 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ services:
class: PHPStan\Stubs\Doctrine\StubFilesExtensionLoader
tags:
- phpstan.stubFilesExtension
arguments:
bleedingEdge: %featureToggles.bleedingEdge%

doctrineQueryBuilderArgumentsProcessor:
class: PHPStan\Type\Doctrine\ArgumentsProcessor
Expand Down Expand Up @@ -434,6 +432,13 @@ services:
tags:
- phpstan.phpDoc.typeNodeResolverExtension

-
class: PHPStan\PhpDoc\Doctrine\DoctrineLiteralStringTypeNodeResolverExtension
arguments:
bleedingEdge: %featureToggles.bleedingEdge%
tags:
- phpstan.phpDoc.typeNodeResolverExtension

-
class: PHPStan\Type\Doctrine\EntityManagerInterfaceThrowTypeExtension
tags:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDoc\Doctrine;

use PHPStan\Analyser\NameScope;
use PHPStan\PhpDoc\TypeNodeResolverExtension;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

class DoctrineLiteralStringTypeNodeResolverExtension implements TypeNodeResolverExtension
{

/** @var bool */
private $bleedingEdge;

public function __construct(bool $bleedingEdge)
{
$this->bleedingEdge = $bleedingEdge;
}

public function resolve(TypeNode $typeNode, NameScope $nameScope): ?Type
{
if (!$typeNode instanceof IdentifierTypeNode) {
return null;
}

if ($typeNode->name !== '__doctrine-literal-string') {
return null;
}

if ($this->bleedingEdge) {
return new IntersectionType([
new StringType(),
new AccessoryLiteralStringType(),
]);
}

return new StringType();
}

}
28 changes: 8 additions & 20 deletions src/Stubs/Doctrine/StubFilesExtensionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPStan\PhpDoc\StubFilesExtension;
use function class_exists;
use function dirname;
use function file_exists;
use function strpos;

class StubFilesExtensionLoader implements StubFilesExtension
Expand All @@ -18,39 +17,28 @@ class StubFilesExtensionLoader implements StubFilesExtension
/** @var Reflector */
private $reflector;

/** @var bool */
private $bleedingEdge;

public function __construct(
Reflector $reflector,
bool $bleedingEdge
Reflector $reflector
)
{
$this->reflector = $reflector;
$this->bleedingEdge = $bleedingEdge;
}

public function getFiles(): array
{
$stubsDir = dirname(dirname(dirname(__DIR__))) . '/stubs';
$path = $stubsDir;

if ($this->bleedingEdge === true) {
$path .= '/bleedingEdge';
}

$files = [];

if (file_exists($path . '/DBAL/Connection4.stub') && $this->isInstalledVersion('doctrine/dbal', 4)) {
$files[] = $path . '/DBAL/Connection4.stub';
$files[] = $path . '/DBAL/ArrayParameterType.stub';
$files[] = $path . '/DBAL/ParameterType.stub';
if ($this->isInstalledVersion('doctrine/dbal', 4)) {
$files[] = $stubsDir . '/DBAL/Connection4.stub';
$files[] = $stubsDir . '/DBAL/ArrayParameterType.stub';
$files[] = $stubsDir . '/DBAL/ParameterType.stub';
} else {
$files[] = $path . '/DBAL/Connection.stub';
$files[] = $stubsDir . '/DBAL/Connection.stub';
}

$files[] = $path . '/ORM/QueryBuilder.stub';
$files[] = $path . '/EntityRepository.stub';
$files[] = $stubsDir . '/ORM/QueryBuilder.stub';
$files[] = $stubsDir . '/EntityRepository.stub';

$hasLazyServiceEntityRepositoryAsParent = false;

Expand Down
File renamed without changes.
56 changes: 56 additions & 0 deletions stubs/DBAL/Connection.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,63 @@

namespace Doctrine\DBAL;

use Doctrine\DBAL\Cache\CacheException;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Types\Type;

class Connection
{
/**
* Executes an SQL statement with the given parameters and returns the number of affected rows.
*
* Could be used for:
* - DML statements: INSERT, UPDATE, DELETE, etc.
* - DDL statements: CREATE, DROP, ALTER, etc.
* - DCL statements: GRANT, REVOKE, etc.
* - Session control statements: ALTER SESSION, SET, DECLARE, etc.
* - Other statements that don't yield a row set.
*
* This method supports PDO binding types as well as DBAL mapping types.
*
* @param __doctrine-literal-string $sql SQL statement
* @param list<mixed>|array<string, mixed> $params Statement parameters
* @param array<int, int|string|Type|null>|array<string, int|string|Type|null> $types Parameter types
*
* @return int|string The number of affected rows.
*
* @throws Exception
*/
public function executeStatement($sql, array $params = [], array $types = []);

/**
* Executes an, optionally parameterized, SQL query.
*
* If the query is parametrized, a prepared statement is used.
* If an SQLLogger is configured, the execution is logged.
*
* @param __doctrine-literal-string $sql SQL query
* @param list<mixed>|array<string, mixed> $params Query parameters
* @param array<int, int|string|Type|null>|array<string, int|string|Type|null> $types Parameter types
*
* @throws Exception
*/
public function executeQuery(
string $sql,
array $params = [],
$types = [],
?QueryCacheProfile $qcp = null
): Result;

/**
* Executes a caching query.
*
* @param __doctrine-literal-string $sql SQL query
* @param list<mixed>|array<string, mixed> $params Query parameters
* @param array<int, int|string|Type|null>|array<string, int|string|Type|null> $types Parameter types
*
* @throws CacheException
* @throws Exception
*/
public function executeCacheQuery($sql, $params, $types, QueryCacheProfile $qcp): Result;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Connection
*
* This method supports PDO binding types as well as DBAL mapping types.
*
* @param literal-string $sql SQL statement
* @param __doctrine-literal-string-string $sql SQL statement
* @param list<mixed>|array<string, mixed> $params Statement parameters
* @param WrapperParameterTypeArray $types Parameter types
*
Expand All @@ -40,7 +40,7 @@ class Connection
* If the query is parametrized, a prepared statement is used.
* If an SQLLogger is configured, the execution is logged.
*
* @param literal-string $sql SQL query
* @param __doctrine-literal-string-string $sql SQL query
* @param list<mixed>|array<string, mixed> $params Query parameters
* @param WrapperParameterTypeArray $types Parameter types
*
Expand All @@ -56,7 +56,7 @@ class Connection
/**
* Executes a caching query.
*
* @param literal-string $sql SQL query
* @param __doctrine-literal-string-string $sql SQL query
* @param list<mixed>|array<string, mixed> $params Query parameters
* @param WrapperParameterTypeArray $types Parameter types
*
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions stubs/EntityRepository.stub
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ class EntityRepository implements ObjectRepository
*/
public function matching(Criteria $criteria);

/**
* @param __doctrine-literal-string $alias
* @param __doctrine-literal-string|null $indexBy
*
* @return QueryBuilder
*/
public function createQueryBuilder($alias, $indexBy = null);

}
134 changes: 134 additions & 0 deletions stubs/ORM/QueryBuilder.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Doctrine\ORM;

use Doctrine\ORM\Query\Expr;

class QueryBuilder
{

Expand All @@ -21,4 +23,136 @@ class QueryBuilder
{
}

/**
* @param string $dqlPartName
* @param __doctrine-literal-string|object|list<__doctrine-literal-string>|array{join: array<int|string, object>} $dqlPart
* @param bool $append
*
* @return $this
*/
public function add($dqlPartName, $dqlPart, $append = false)
{

}

/**
* @param __doctrine-literal-string|null $delete
* @param __doctrine-literal-string|null $alias
*
* @return $this
*/
public function delete($delete = null, $alias = null)
{

}

/**
* @param __doctrine-literal-string|null $update
* @param __doctrine-literal-string|null $alias
*
* @return $this
*/
public function update($update = null, $alias = null)
{

}

/**
* @param __doctrine-literal-string|class-string $from
* @param __doctrine-literal-string $alias
* @param __doctrine-literal-string|null $indexBy
*
* @return $this
*/
public function from($from, $alias, $indexBy = null)
{

}

/**
* @param __doctrine-literal-string|class-string $join
* @param __doctrine-literal-string $alias
* @param Expr\Join::ON|Expr\Join::WITH|null $conditionType
* @param __doctrine-literal-string|Expr\Comparison|Expr\Composite|Expr\Func|null $condition
* @param __doctrine-literal-string|null $indexBy
*
* @return $this
*/
public function innerJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
{

}

/**
* @param __doctrine-literal-string|class-string $join
* @param __doctrine-literal-string $alias
* @param Expr\Join::ON|Expr\Join::WITH|null $conditionType
* @param __doctrine-literal-string|Expr\Comparison|Expr\Composite|Expr\Func|null $condition
* @param __doctrine-literal-string|null $indexBy
*
* @return $this
*/
public function leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
{

}

/**
* @param __doctrine-literal-string|class-string $join
* @param __doctrine-literal-string $alias
* @param Expr\Join::ON|Expr\Join::WITH|null $conditionType
* @param __doctrine-literal-string|Expr\Comparison|Expr\Composite|Expr\Func|null $condition
* @param __doctrine-literal-string|null $indexBy
*
* @return $this
*/
public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
{

}

/**
* @return __doctrine-literal-string
*/
public function getRootAlias()
{

}

/**
* @return list<__doctrine-literal-string>
*/
public function getRootAliases()
{

}

/**
* @return list<__doctrine-literal-string>
*/
public function getAllAlias()
{

}

/**
* @param __doctrine-literal-string|object|array<mixed> $predicates
* @return $this
*/
public function where($predicates)
{

}

/**
* @param __doctrine-literal-string|object|array<mixed> $predicates
* @return $this
*/
public function andWhere($predicates)
{

}



}
Loading

0 comments on commit e71f08d

Please sign in to comment.