Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make implicit type casts explicit #8836

Merged
merged 1 commit into from
Jul 13, 2021
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
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Cache/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public function __construct(string $value, ?int $time = null)
*/
public static function createLockRead()
{
return new self(uniqid(time(), true));
return new self(uniqid((string) time(), true));
}
}
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class): void

$sequenceGenerator = new SequenceGenerator(
$this->em->getConfiguration()->getQuoteStrategy()->getSequenceName($definition, $class, $this->getTargetPlatform()),
$definition['allocationSize']
(int) $definition['allocationSize']
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the + operator is used here, this must be fine:

$this->_maxValue = $this->_nextValue + $this->_allocationSize;

);
$class->setIdGenerator($sequenceGenerator);
break;
Expand Down
15 changes: 9 additions & 6 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,13 @@ class ClassMetadataInfo implements ClassMetadata
* <code>
* array(
* 'sequenceName' => 'name',
* 'allocationSize' => 20,
* 'initialValue' => 1
* 'allocationSize' => '20',
* 'initialValue' => '1'
* )
* </code>
*
* @var mixed[]
* @psalm-var array{sequenceName: string, allocationSize: int, initialValue: int}
* @psalm-var array{sequenceName: string, allocationSize: string, initialValue: string, quoted?: mixed}
* @todo Merge with tableGeneratorDefinition into generic generatorDefinition
*/
public $sequenceGeneratorDefinition;
Expand Down Expand Up @@ -3289,7 +3289,7 @@ public function setCustomGeneratorDefinition(array $definition)
* )
* </code>
*
* @psalm-param array<string, string> $definition
* @psalm-param array{sequenceName?: string, allocationSize?: int|string, initialValue?: int|string, quoted?: mixed} $definition
*
* @return void
*
Expand All @@ -3306,14 +3306,17 @@ public function setSequenceGeneratorDefinition(array $definition)
$definition['quoted'] = true;
}

if (! isset($definition['allocationSize']) || trim($definition['allocationSize']) === '') {
if (! isset($definition['allocationSize']) || trim((string) $definition['allocationSize']) === '') {
$definition['allocationSize'] = '1';
}

if (! isset($definition['initialValue']) || trim($definition['initialValue']) === '') {
if (! isset($definition['initialValue']) || trim((string) $definition['initialValue']) === '') {
$definition['initialValue'] = '1';
}

$definition['allocationSize'] = (string) $definition['allocationSize'];
$definition['initialValue'] = (string) $definition['initialValue'];

Comment on lines +3309 to +3319
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would make sense to use a variable here now so you need to cast only one time. It increases readability after the type casts were introduced or when changes are going to be introduced where the type casts are forgotten.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please show me how you would do it? It tried, and it's a bit too tricky IMO, since $definition['allocationSize'] might not be defined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I confused keys during my review.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries :)

$this->sequenceGeneratorDefinition = $definition;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ private function columnToArray(SimpleXMLElement $fieldMapping): array
private function cacheToArray(SimpleXMLElement $cacheMapping): array
{
$region = isset($cacheMapping['region']) ? (string) $cacheMapping['region'] : null;
$usage = isset($cacheMapping['usage']) ? strtoupper($cacheMapping['usage']) : null;
$usage = isset($cacheMapping['usage']) ? strtoupper((string) $cacheMapping['usage']) : null;

if ($usage && ! defined('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage)) {
throw new InvalidArgumentException(sprintf('Invalid cache usage "%s"', $usage));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function containsKey(PersistentCollection $collection, $key)

[$quotedJoinTable, $whereClauses, $params, $types] = $this->getJoinTableRestrictionsWithKey(
$collection,
$key,
(string) $key,
true
);

Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Query/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public function syntaxError($expected = '', $token = null)
$message .= $expected !== '' ? sprintf('Expected %s, got ', $expected) : 'Unexpected ';
$message .= $this->lexer->lookahead === null ? 'end of string.' : sprintf("'%s'", $token['value']);

throw QueryException::syntaxError($message, QueryException::dqlError($this->query->getDQL()));
throw QueryException::syntaxError($message, QueryException::dqlError($this->query->getDQL() ?? ''));
SenseException marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -524,7 +524,7 @@ public function semanticalError($message = '', $token = null)
$length = $pos !== false ? $pos - $token['position'] : $distance;

$tokenPos = isset($token['position']) && $token['position'] > 0 ? $token['position'] : '-1';
$tokenStr = substr($dql, $token['position'], $length);
$tokenStr = substr($dql, (int) $token['position'], $length);

// Building informative message
$message = 'line 0, col ' . $tokenPos . " near '" . $tokenStr . "': Error: " . $message;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2120,7 +2120,7 @@ public function walkLiteral($literal)
return $this->conn->getDatabasePlatform()->convertBooleans(strtolower($literal->value) === 'true');

case AST\Literal::NUMERIC:
return $literal->value;
return (string) $literal->value;

default:
throw QueryException::invalidLiteral($literal);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ public function join($join, $alias, $conditionType = null, $condition = null, $i
*/
public function innerJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
{
$parentAlias = substr($join, 0, strpos($join, '.'));
$parentAlias = substr($join, 0, (int) strpos($join, '.'));

$rootAlias = $this->findRootAlias($alias, $parentAlias);

Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Tools/ConvertDoctrine1Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@ private function convertColumn(
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);

$definition = [
'sequenceName' => is_array($column['sequence']) ? $column['sequence']['name'] : $column['sequence'],
'sequenceName' => (string) (is_array($column['sequence']) ? $column['sequence']['name'] : $column['sequence']),
];

if (isset($column['sequence']['size'])) {
$definition['allocationSize'] = $column['sequence']['size'];
$definition['allocationSize'] = (int) $column['sequence']['size'];
}

if (isset($column['sequence']['value'])) {
$definition['initialValue'] = $column['sequence']['value'];
$definition['initialValue'] = (int) $column['sequence']['value'];
}

$metadata->setSequenceGeneratorDefinition($definition);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function generateEntityRepositoryClass($fullClassName)
*/
private function getClassNamespace(string $fullClassName): string
{
return substr($fullClassName, 0, strrpos($fullClassName, '\\'));
return substr($fullClassName, 0, (int) strrpos($fullClassName, '\\'));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function exportClassMetadata(ClassMetadataInfo $metadata)
}

if (isset($field['length'])) {
$fieldXml->addAttribute('length', $field['length']);
$fieldXml->addAttribute('length', (string) $field['length']);
}

if (isset($field['precision'])) {
Expand All @@ -200,7 +200,7 @@ public function exportClassMetadata(ClassMetadataInfo $metadata)
if (isset($field['options'])) {
$optionsXml = $fieldXml->addChild('options');
foreach ($field['options'] as $key => $value) {
$optionXml = $optionsXml->addChild('option', $value);
$optionXml = $optionsXml->addChild('option', (string) $value);
$optionXml->addAttribute('name', $key);
}
}
Expand Down Expand Up @@ -230,7 +230,7 @@ public function exportClassMetadata(ClassMetadataInfo $metadata)
$a1 = array_search($m1['type'], $orderMap);
$a2 = array_search($m2['type'], $orderMap);

return strcmp($a1, $a2);
return strcmp((string) $a1, (string) $a2);
});

foreach ($metadata->associationMappings as $associationMapping) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Tools/SchemaTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ static function (ClassMetadata $class) use ($idMapping): bool {
if (! $schema->hasSequence($quotedName)) {
$schema->createSequence(
$quotedName,
$seqDef['allocationSize'],
$seqDef['initialValue']
(int) $seqDef['allocationSize'],
(int) $seqDef['initialValue']
);
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/Doctrine/ORM/Tools/ToolsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class ToolsException extends ORMException
{
public static function schemaToolFailure(string $sql, Throwable $e): self
{
return new self("Schema-Tool failed with Error '" . $e->getMessage() . "' while executing DDL: " . $sql, '0', $e);
return new self(
"Schema-Tool failed with Error '" . $e->getMessage() . "' while executing DDL: " . $sql,
0,
$e
);
}

/**
Expand Down
42 changes: 1 addition & 41 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ parameters:
count: 1
path: lib/Doctrine/ORM/Cache/DefaultQueryCache.php

-
message: "#^Parameter \\#1 \\$prefix of function uniqid expects string, int given\\.$#"
count: 1
path: lib/Doctrine/ORM/Cache/Lock.php

-
message: "#^Access to an undefined property Doctrine\\\\ORM\\\\Cache\\\\CacheEntry\\:\\:\\$identifiers\\.$#"
count: 1
Expand Down Expand Up @@ -450,16 +445,6 @@ parameters:
count: 1
path: lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php

-
message: "#^Parameter \\#1 \\$definition of method Doctrine\\\\ORM\\\\Mapping\\\\ClassMetadataInfo\\<object\\>\\:\\:setSequenceGeneratorDefinition\\(\\) expects array\\<string, string\\>, array\\<string, int\\|string\\> given\\.$#"
count: 1
path: lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php

-
message: "#^Parameter \\#1 \\$definition of method Doctrine\\\\ORM\\\\Mapping\\\\ClassMetadataInfo\\<object\\>\\:\\:setSequenceGeneratorDefinition\\(\\) expects array\\<string, string\\>, array\\<string, int\\|string\\|true\\> given\\.$#"
count: 1
path: lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php

-
message: "#^Parameter \\#1 \\$subClass of method Doctrine\\\\ORM\\\\Mapping\\\\ClassMetadataFactory\\:\\:addInheritedEmbeddedClasses\\(\\) expects Doctrine\\\\ORM\\\\Mapping\\\\ClassMetadata, Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata given\\.$#"
count: 1
Expand Down Expand Up @@ -590,11 +575,6 @@ parameters:
count: 1
path: lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php

-
message: "#^Property Doctrine\\\\ORM\\\\Mapping\\\\ClassMetadataInfo\\<T of object\\>\\:\\:\\$sequenceGeneratorDefinition \\(array\\('sequenceName' \\=\\> string, 'allocationSize' \\=\\> int, 'initialValue' \\=\\> int\\)\\) does not accept array\\<string, string\\|true\\>\\.$#"
count: 1
path: lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php

-
message: "#^Result of && is always false\\.$#"
count: 2
Expand Down Expand Up @@ -795,11 +775,6 @@ parameters:
count: 2
path: lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php

-
message: "#^Parameter \\#1 \\$definition of method Doctrine\\\\ORM\\\\Mapping\\\\ClassMetadataInfo\\<object\\>\\:\\:setSequenceGeneratorDefinition\\(\\) expects array\\<string, string\\>, array\\<string, int\\|string\\> given\\.$#"
count: 1
path: lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php

-
message: "#^Parameter \\#1 \\$metadata of static method Doctrine\\\\ORM\\\\Mapping\\\\Builder\\\\EntityListenerBuilder\\:\\:bindEntityListener\\(\\) expects Doctrine\\\\ORM\\\\Mapping\\\\ClassMetadata, Doctrine\\\\ORM\\\\Mapping\\\\ClassMetadataInfo&Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\<T of object\\> given\\.$#"
count: 1
Expand Down Expand Up @@ -2161,24 +2136,14 @@ parameters:
count: 1
path: lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php

-
message: "#^Parameter \\#1 \\$str1 of function strcmp expects string, int\\|false given\\.$#"
count: 1
path: lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php

-
message: "#^Parameter \\#1 \\$type of method Doctrine\\\\ORM\\\\Tools\\\\Export\\\\Driver\\\\AbstractExporter\\:\\:_getIdGeneratorTypeString\\(\\) expects 1\\|2\\|3\\|4\\|5\\|6\\|7, int given\\.$#"
count: 2
path: lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php

-
message: "#^Parameter \\#2 \\$str2 of function strcmp expects string, int\\|false given\\.$#"
count: 1
path: lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php

-
message: "#^Parameter \\#2 \\$value of method SimpleXMLElement\\:\\:addAttribute\\(\\) expects string, int given\\.$#"
count: 6
count: 3
path: lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php

-
Expand Down Expand Up @@ -2236,11 +2201,6 @@ parameters:
count: 1
path: lib/Doctrine/ORM/Tools/SchemaTool.php

-
message: "#^Parameter \\#2 \\$code of class Doctrine\\\\ORM\\\\Tools\\\\ToolsException constructor expects int, string given\\.$#"
count: 1
path: lib/Doctrine/ORM/Tools/ToolsException.php

-
message: "#^Binary operation \"&\" between string and 3 results in an error\\.$#"
count: 1
Expand Down
32 changes: 3 additions & 29 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,6 @@
<code>loadCacheEntry</code>
</MissingReturnType>
</file>
<file src="lib/Doctrine/ORM/Cache/Lock.php">
<InvalidScalarArgument occurrences="1">
<code>time()</code>
</InvalidScalarArgument>
</file>
<file src="lib/Doctrine/ORM/Cache/Logging/CacheLogger.php">
<MissingReturnType occurrences="9">
<code>collectionCacheHit</code>
Expand Down Expand Up @@ -911,10 +906,6 @@
<InvalidArrayOffset occurrences="1">
<code>$subClass-&gt;table[$indexType][$indexName]</code>
</InvalidArrayOffset>
<InvalidScalarArgument occurrences="2">
<code>$definition</code>
<code>$parent-&gt;sequenceGeneratorDefinition</code>
</InvalidScalarArgument>
<MissingConstructor occurrences="2">
<code>$driver</code>
<code>$evm</code>
Expand Down Expand Up @@ -1025,9 +1016,6 @@
<code>ReflectionProperty</code>
<code>ReflectionProperty</code>
</InvalidNullableReturnType>
<InvalidPropertyAssignmentValue occurrences="1">
<code>$definition</code>
</InvalidPropertyAssignmentValue>
<InvalidScalarArgument occurrences="1">
<code>$type</code>
</InvalidScalarArgument>
Expand Down Expand Up @@ -1262,7 +1250,6 @@
<code>$value[1]</code>
<code>$value[1]</code>
</InvalidArrayAccess>
<InvalidScalarArgument occurrences="1"/>
<NonInvariantDocblockPropertyType occurrences="1">
<code>$entityAnnotationClasses</code>
</NonInvariantDocblockPropertyType>
Expand Down Expand Up @@ -1351,9 +1338,6 @@
<ArgumentTypeCoercion occurrences="1">
<code>$metadata</code>
</ArgumentTypeCoercion>
<ImplicitToStringCast occurrences="1">
<code>$cacheMapping['usage']</code>
</ImplicitToStringCast>
<LessSpecificReturnStatement occurrences="1"/>
<MissingParamType occurrences="2">
<code>$fileExtension</code>
Expand Down Expand Up @@ -2643,7 +2627,7 @@
<code>$this-&gt;ConditionalExpression()</code>
<code>$this-&gt;ConditionalExpression()</code>
</PossiblyInvalidPropertyAssignmentValue>
<PossiblyNullArgument occurrences="30">
<PossiblyNullArgument occurrences="29">
<code>$aliasIdentVariable</code>
<code>$dql</code>
<code>$dql</code>
Expand Down Expand Up @@ -3196,7 +3180,7 @@
<NullableReturnStatement occurrences="1">
<code>$this-&gt;cacheMode</code>
</NullableReturnStatement>
<PossiblyFalseArgument occurrences="4">
<PossiblyFalseArgument occurrences="3">
<code>$spacePos</code>
<code>$spacePos</code>
<code>strpos($join, '.')</code>
Expand Down Expand Up @@ -3604,9 +3588,6 @@
<DocblockTypeContradiction occurrences="1">
<code>EntityRepository::class</code>
</DocblockTypeContradiction>
<PossiblyFalseArgument occurrences="1">
<code>strrpos($fullClassName, '\\')</code>
</PossiblyFalseArgument>
<PossiblyFalseOperand occurrences="1">
<code>strrpos($fullClassName, '\\')</code>
</PossiblyFalseOperand>
Expand Down Expand Up @@ -3686,13 +3667,11 @@
<DeprecatedClass occurrences="1">
<code>AbstractExporter</code>
</DeprecatedClass>
<InvalidScalarArgument occurrences="6">
<InvalidScalarArgument occurrences="3">
<code>$field['length']</code>
<code>$field['length']</code>
<code>$field['precision']</code>
<code>$field['scale']</code>
<code>$sequenceDefinition['allocationSize']</code>
<code>$sequenceDefinition['initialValue']</code>
</InvalidScalarArgument>
<MissingClosureParamType occurrences="2">
<code>$m1</code>
Expand Down Expand Up @@ -3862,11 +3841,6 @@
<code>require_once $directory . '/Doctrine/Common/ClassLoader.php'</code>
</UnresolvableInclude>
</file>
<file src="lib/Doctrine/ORM/Tools/ToolsException.php">
<InvalidScalarArgument occurrences="1">
<code>'0'</code>
</InvalidScalarArgument>
</file>
<file src="lib/Doctrine/ORM/UnitOfWork.php">
<ArgumentTypeCoercion occurrences="6">
<code>$class</code>
Expand Down
Loading