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

Use of "iterable" Type May Need Re-Evaluation Across the Repo #783

Merged
merged 14 commits into from
Jul 20, 2022
Merged
19 changes: 19 additions & 0 deletions src/SDK/Common/Util/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OpenTelemetry\SDK\Common\Util;

use Closure;
use Exception;
use function get_class;
use ReflectionFunction;
use stdClass;
Expand Down Expand Up @@ -50,3 +51,21 @@ function weaken(Closure $closure, ?object &$target = null): Closure
? static fn (...$args) => ($obj = $ref->get()) ? $closure->call($obj, ...$args) : null
: static fn (...$args) => ($obj = $ref->get()) ? $closure->bindTo($obj)(...$args) : null;
}

/**
* Returns whether an iterable is empty or not
*/
function isEmpty(iterable $list)
{
try {
foreach ($list as $value) {
break;
}
}
//catch exception
catch (Exception $e) {
return true;
}

return empty($list);
}
3 changes: 2 additions & 1 deletion src/SDK/Metrics/Exporters/AbstractExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Exception;
use InvalidArgumentException;
use OpenTelemetry\API\Metrics as API;
use function OpenTelemetry\SDK\Common\Util\isEmpty;
use OpenTelemetry\SDK\Metrics\Exceptions\RetryableExportException;

abstract class AbstractExporter implements API\ExporterInterface
Expand All @@ -16,7 +17,7 @@ abstract class AbstractExporter implements API\ExporterInterface
*/
public function export(iterable $metrics): int
{
if (empty($metrics)) {
if (isEmpty($metrics)) {
return API\ExporterInterface::SUCCESS;
}

Expand Down
3 changes: 2 additions & 1 deletion src/SDK/Trace/Behavior/SpanExporterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace OpenTelemetry\SDK\Trace\Behavior;

use function OpenTelemetry\SDK\Common\Util\isEmpty;
use OpenTelemetry\SDK\Trace\SpanDataInterface;
use OpenTelemetry\SDK\Trace\SpanExporterInterface;

Expand Down Expand Up @@ -40,7 +41,7 @@ public function export(iterable $spans): int
return SpanExporterInterface::STATUS_FAILED_NOT_RETRYABLE;
}

if (empty($spans)) {
if (isEmpty($spans)) {
return SpanExporterInterface::STATUS_SUCCESS;
}

Expand Down