Skip to content

Commit

Permalink
add is_iterable support
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Aug 6, 2017
1 parent 821f3dc commit 926e188
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"php": "^7.0",
"doctrine/common": "^2.0",
"doctrine/inflector": "^1.0",
"symfony/polyfill-php71": "^1.0",
"zendframework/zend-paginator": "^2.6"
},
"require-dev": {
Expand Down Expand Up @@ -56,29 +57,28 @@
"bin": [
],
"config": {
"preferred-install": "dist"
"preferred-install": "dist",
"sort-packages": true
},
"scripts": {
"php-lint": "php -l src && php -l tests",
"phplint": "php -l src && php -l tests",
"phpcs": "phpcs --standard=PSR2 src tests",
"phpcs-lint": "php-cs-fixer fix --dry-run --verbose",
"phpcpd": "phpcpd src",
"phpstan": "phpstan analyse --level 1 src tests",
"phpmd": "phpmd src text unusedcode,naming,design,controversial,codesize",
"phpmetrics": "phpmetrics --failure-condition='average.maintainabilityIndex < 50' src",
"phpmetrics-report": "phpmetrics --report-html=build/metrics/index.html --offline src",
"phpmetrics-report": "phpmetrics --report-html=build/metrics/index.html --report-violations=build/logs/violations.xml --offline src",
"phpstan": "phpstan analyse --level 7 src",
"phpunit": "phpunit",
"phpunit-coverage": "phpunit --coverage-html build/coverage",
"phpunit-clover": "phpunit --coverage-clover build/logs/clover.xml",
"humbug": "humbug",
"qa": [
"@php-lint",
"@phplint",
"@phpcs",
"@phpcs-lint",
"@phpcpd",
"@phpmd",
"@phpstan",
"@phpmetrics"
"@phpstan"
],
"reports": [
"@phpmetrics-report",
Expand All @@ -87,7 +87,7 @@
"fix": "php-cs-fixer fix --verbose",
"security": "composer outdated",
"test": [
"@php-lint",
"@qa",
"@phpunit",
"@humbug"
]
Expand Down
12 changes: 6 additions & 6 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public function getNew();
/**
* Add objects.
*
* @param object|object[]|\Traversable $objects
* @param bool $flush
* @param object|iterable $objects
* @param bool $flush
*/
public function add($objects, bool $flush = false);

Expand Down Expand Up @@ -161,22 +161,22 @@ public function removeOneBy(array $criteria, bool $flush = false);
/**
* Remove objects.
*
* @param object|object[]|\Traversable|string|int $objects
* @param bool $flush
* @param object|iterable|string|int $objects
* @param bool $flush
*/
public function remove($objects, bool $flush = false);

/**
* Refresh objects.
*
* @param object|object[]|\Traversable $objects
* @param object|iterable $objects
*/
public function refresh($objects);

/**
* Detach objects.
*
* @param object|object[]|\Traversable $objects
* @param object|iterable $objects
*/
public function detach($objects);

Expand Down
52 changes: 20 additions & 32 deletions src/RepositoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public function setObjectFactory(callable $objectFactory)
/**
* Add objects.
*
* @param object|object[]|\Traversable $objects
* @param bool $flush
* @param object|iterable $objects
* @param bool $flush
*
* @throws \InvalidArgumentException
*/
Expand Down Expand Up @@ -201,14 +201,14 @@ public function removeOneBy(array $criteria, bool $flush = false)
/**
* Remove objects.
*
* @param object|object[]|\Traversable|string|int $objects
* @param bool $flush
* @param object|iterable|string|int $objects
* @param bool $flush
*
* @throws \InvalidArgumentException
*/
public function remove($objects, bool $flush = false)
{
if (!is_object($objects) && !is_array($objects) && !$objects instanceof \Traversable) {
if (!is_object($objects) && !is_iterable($objects)) {
$objects = $this->find($objects);
}

Expand All @@ -218,7 +218,7 @@ public function remove($objects, bool $flush = false)
/**
* Refresh objects.
*
* @param object|object[]|\Traversable $objects
* @param object|iterable $objects
*
* @throws \InvalidArgumentException
*/
Expand All @@ -235,7 +235,7 @@ public function refresh($objects)
/**
* Detach objects.
*
* @param object|object[]|\Traversable $objects
* @param object|iterable $objects
*
* @throws \InvalidArgumentException
*/
Expand Down Expand Up @@ -362,17 +362,17 @@ protected function callSupportedMethod(string $method, string $fieldName, array
/**
* Run manager action.
*
* @param string $action
* @param object|object[]|\Traversable $objects
* @param bool $flush
* @param string $action
* @param object|iterable $objects
* @param bool $flush
*
* @throws \InvalidArgumentException
*/
protected function runManagerAction(string $action, $objects, bool $flush)
{
$manager = $this->getManager();

if (!$this->isTraversable($objects)) {
if (!is_iterable($objects)) {
$objects = array_filter([$objects]);
}

Expand All @@ -390,24 +390,24 @@ protected function runManagerAction(string $action, $objects, bool $flush)
$manager->$action($object);
}

// @codeCoverageIgnoreStart
if ($objects instanceof \Traversable) {
$objects = iterator_to_array($objects);
}
// @codeCoverageIgnoreEnd

$this->flushObjects($objects, $flush);
}

/**
* Flush managed objects.
*
* @param object|object[]|\Traversable $objects
* @param bool $flush
* @param array $objects
* @param bool $flush
*/
protected function flushObjects($objects, bool $flush)
protected function flushObjects(array $objects, bool $flush)
{
if ($flush || $this->autoFlush) {
// @codeCoverageIgnoreStart
if ($objects instanceof \Traversable) {
$objects = iterator_to_array($objects);
}
// @codeCoverageIgnoreEnd

$this->getManager()->flush($objects);
}
}
Expand Down Expand Up @@ -446,16 +446,4 @@ abstract protected function getManager();
* @return \Doctrine\Common\Persistence\Mapping\ClassMetadata
*/
abstract protected function getClassMetadata();

/**
* Is traversable.
*
* @param mixed $object
*
* @return bool
*/
private function isTraversable($object): bool
{
return is_array($object) || $object instanceof \Traversable;
}
}

0 comments on commit 926e188

Please sign in to comment.