Skip to content

Commit

Permalink
Merge pull request doctrine#2231 from franmomu/update_phpstan
Browse files Browse the repository at this point in the history
Update PHPStan
  • Loading branch information
malarzm authored Nov 4, 2020
2 parents 91f71db + e718fcc commit 519403e
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ jobs:
run: "composer install --no-interaction --no-progress --no-suggest"

- name: "Run a static analysis with phpstan/phpstan"
run: "vendor/bin/phpstan analyse"
run: "vendor/bin/phpstan analyse --error-format=github"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"doctrine/coding-standard": "^6.0",
"jmikola/geojson": "^1.0",
"phpbench/phpbench": "^0.13.0",
"phpstan/phpstan": "^0.10.3",
"phpstan/phpstan": "^0.12.32",
"phpunit/phpunit": "^8.2",
"squizlabs/php_codesniffer": "^3.5, <3.5.5"
},
Expand Down
15 changes: 10 additions & 5 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ public function getClassNameResolver() : ClassNameResolver
*/
public function getClassMetadata($className) : ClassMetadata
{
return $this->metadataFactory->getMetadataFor($className);
$metadata = $this->metadataFactory->getMetadataFor($className);
assert($metadata instanceof ClassMetadata);

return $metadata;
}

/**
Expand Down Expand Up @@ -340,9 +343,9 @@ public function getDocumentCollection(string $className) : Collection
{
$className = $this->classNameResolver->getRealClass($className);

/** @var ClassMetadata $metadata */
$metadata = $this->metadataFactory->getMetadataFor($className);
assert($metadata instanceof ClassMetadata);

if ($metadata->isFile) {
return $this->getDocumentBucket($className)->getFilesCollection();
}
Expand Down Expand Up @@ -376,8 +379,9 @@ public function getDocumentBucket(string $className) : Bucket
{
$className = $this->classNameResolver->getRealClass($className);

/** @var ClassMetadata $metadata */
$metadata = $this->metadataFactory->getMetadataFor($className);
assert($metadata instanceof ClassMetadata);

if (! $metadata->isFile) {
throw MongoDBException::documentBucketOnlyAvailableForGridFSFiles($className);
}
Expand Down Expand Up @@ -587,8 +591,8 @@ public function flush(array $options = [])
*/
public function getReference(string $documentName, $identifier) : object
{
/** @var ClassMetadata $class */
$class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\'));
$class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\'));
assert($class instanceof ClassMetadata);
$document = $this->unitOfWork->tryGetById($identifier, $class);

// Check identity map first, if its already in there just return it.
Expand Down Expand Up @@ -623,6 +627,7 @@ public function getPartialReference(string $documentName, $identifier) : object
{
$class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\'));
assert($class instanceof ClassMetadata);

$document = $this->unitOfWork->tryGetById($identifier, $class);

// Check identity map first, if its already in there just return it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public function count()
*/
public function set($key, $value)
{
return $this->doSet($key, $value, false);
$this->doSet($key, $value, false);
}

/**
Expand Down Expand Up @@ -624,7 +624,9 @@ public function offsetGet($offset)
public function offsetSet($offset, $value)
{
if (! isset($offset)) {
return $this->doAdd($value, true);
$this->doAdd($value, true);

return;
}

$this->doSet($offset, $value, true);
Expand Down Expand Up @@ -698,7 +700,7 @@ public function __clone()
* @param mixed $value
* @param bool $arrayAccess
*
* @return bool
* @return true
*/
private function doAdd($value, $arrayAccess)
{
Expand Down
20 changes: 9 additions & 11 deletions lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,12 @@ public function executeInserts(array $options = []) : void
$inserts[] = $data;
}

if ($inserts) {
try {
assert($this->collection instanceof Collection);
$this->collection->insertMany($inserts, $options);
} catch (DriverException $e) {
$this->queuedInserts = [];
throw $e;
}
try {
assert($this->collection instanceof Collection);
$this->collection->insertMany($inserts, $options);
} catch (DriverException $e) {
$this->queuedInserts = [];
throw $e;
}

/* All collections except for ones using addToSet have already been
Expand Down Expand Up @@ -614,9 +612,9 @@ public function unlock(object $document) : void
* @param object $document The document object to fill, if any.
* @param array $hints Hints for document creation.
*
* @return object|null The filled and managed document object or NULL, if the query result is empty.
* @return object The filled and managed document object.
*/
private function createDocument(array $result, ?object $document = null, array $hints = []) : ?object
private function createDocument(array $result, ?object $document = null, array $hints = []) : object
{
if ($document !== null) {
$hints[Query::HINT_REFRESH] = true;
Expand Down Expand Up @@ -885,7 +883,7 @@ public function prepareProjection(array $fields) : array
}

/**
* @param int|string $sort
* @param int|string|null $sort
*
* @return int|string|null
*/
Expand Down
8 changes: 2 additions & 6 deletions lib/Doctrine/ODM/MongoDB/Query/ReferencePrimer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use function explode;
use function get_class;
use function implode;
use function is_callable;
use function is_object;
use function serialize;
use function sprintf;
Expand Down Expand Up @@ -101,7 +100,6 @@ public function __construct(DocumentManager $dm, UnitOfWork $uow)
*
* @throws InvalidArgumentException If the mapped field is not the owning
* side of a reference relationship.
* @throws InvalidArgumentException If $primer is not callable.
* @throws LogicException If the mapped field is a simple reference and is
* missing a target document class.
*/
Expand All @@ -127,10 +125,6 @@ public function primeReferences(ClassMetadata $class, $documents, string $fieldN
throw new LogicException(sprintf('Field "%s" is an identifier reference without a target document class in class "%s"', $fieldName, $class->name));
}

if ($primer !== null && ! is_callable($primer)) {
throw new InvalidArgumentException('$primer is not callable');
}

$primer = $primer ?: $this->defaultPrimer;
$groupedIds = [];

Expand Down Expand Up @@ -232,6 +226,8 @@ private function parseDotSyntaxForPrimer(string $fieldName, ClassMetadata $class

return ['fieldName' => $fieldName, 'class' => $class, 'documents' => $documents, 'mapping' => $mapping];
}

throw new LogicException('Unable to parse property path for ReferencePrimer. Please report an issue in Doctrine\'s MongoDB ODM.');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ protected function createRepository(DocumentManager $documentManager, string $do

case $metadata->isEmbeddedDocument:
throw MongoDBException::cannotCreateRepository($documentName);
break;

case $metadata->isMappedSuperclass:
default:
Expand Down
22 changes: 22 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
parameters:
ignoreErrors:
-
message: "#^PHPDoc tag @param references unknown parameter\\: \\$applyFilters$#"
count: 1
path: lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php

-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 1
path: lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php

-
message: "#^Return type \\(Doctrine\\\\ODM\\\\MongoDB\\\\Aggregation\\\\Stage\\\\GeoNear\\) of method Doctrine\\\\ODM\\\\MongoDB\\\\Aggregation\\\\Stage\\\\GeoNear\\:\\:limit\\(\\) should be compatible with return type \\(Doctrine\\\\ODM\\\\MongoDB\\\\Aggregation\\\\Stage\\\\Limit\\) of method Doctrine\\\\ODM\\\\MongoDB\\\\Aggregation\\\\Stage\\:\\:limit\\(\\)$#"
count: 1
path: lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GeoNear.php

-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 1
path: lib/Doctrine/ODM/MongoDB/DocumentManager.php

10 changes: 5 additions & 5 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
includes:
- phpstan-baseline.neon

parameters:
level: 7
treatPhpDocTypesAsCertain: false
level: 5
paths:
- %rootDir%/../../../benchmark
- %rootDir%/../../../lib

ignoreErrors:
# See https://github.com/phpstan/phpstan/pull/1730
- '#^Parameter \#1 $wstring of class MongoDB\\Driver\\WriteConcern constructor expects string, int|string given\.$#'

0 comments on commit 519403e

Please sign in to comment.