opentracing / opentracing-php

OpenTracing API for PHP
Apache License 2.0
505 stars 56 forks source link

MockTracer::inject passes carrier by value rather than by reference to injector function #106

Closed mszabo-wikia closed 4 years ago

mszabo-wikia commented 4 years ago

Background

The MockTracer class provided by opentracing-php allows users to provide callables implementing tracing context injection for a given format. The inject() method implementation, when called with a given context, format and carrier, looks up the user-provided callable for the given format, and invokes it via PHP's call_user_func function to perform the injection.

Problem

This implementation is broken, because call_user_func passes parameters to the target by value rather than by reference.[1] Since the injection process relies on the carrier being passed by reference, so that the injection callable can modify the carrier, this will cause a PHP Warning and cause the injection to fail, e.g.:

Parameter 2 to OpenTracing\Tests\Mock\MockTracerTest::OpenTracing\Tests\Mock\{closure}() expected to be a reference, value given

Proposal

A solution is to have MockTracer invoke the callable directly, i.e.

call_user_func($this->injectors[$format], $spanContext, $carrier);

would become:

$this->injectors[$format]($spanContext, $carrier);

This would ensure that $carrier is passed by reference to the callable.


[1] https://www.php.net/manual/en/function.call-user-func.php