/** * Creates a parameters signature of a `ReflectionMethod` instance. * * @param object $method A instance of `ReflectionMethod`. * @return string The parameters definition list. */ protected static function _generateSignature($method) { $params = []; $isVariadic = Suite::$PHP >= 7 ? $method->isVariadic() : false; foreach ($method->getParameters() as $num => $parameter) { $typehint = Inspector::typehint($parameter); $name = $parameter->getName(); $name = $name && $name !== '...' ? $name : 'param' . $num; $reference = $parameter->isPassedByReference() ? '&' : ''; $default = ''; if ($parameter->isDefaultValueAvailable()) { $default = var_export($parameter->getDefaultValue(), true); $default = ' = ' . preg_replace('/\\s+/', '', $default); } elseif ($parameter->isOptional()) { if ($isVariadic && $parameter->isVariadic()) { $reference = '...'; $default = ''; } else { $default = ' = NULL'; } } $typehint = $typehint ? $typehint . ' ' : $typehint; $params[] = "{$typehint}{$reference}\${$name}{$default}"; } return join(', ', $params); }
expect($param4->getName())->toBe('d'); expect($param4->getDefaultValue())->toBe(null); }); it("merges defauts values with populated values when the third argument is not empty", function () { $inspector = Inspector::parameters($this->class, 'parametersExample', ['first', 1000, true]); expect($inspector)->toBe(['a' => 'first', 'b' => 1000, 'c' => true, 'd' => null]); }); }); describe("::typehint()", function () { it("returns an empty string when no typehint is present", function () { $inspector = Inspector::parameters($this->class, 'parametersExample'); expect(Inspector::typehint($inspector[0]))->toBe(''); $inspector = Inspector::parameters($this->class, 'parameterByReference'); expect(Inspector::typehint($inspector[0]))->toBe(''); }); it("returns parameter typehint", function () { $inspector = Inspector::parameters($this->class, 'exceptionTypeHint'); $typehint = Inspector::typehint(current($inspector)); expect($typehint)->toBeA('string'); expect($typehint)->toBe('\\Exception'); $inspector = Inspector::parameters($this->class, 'arrayTypeHint'); $typehint = Inspector::typehint(current($inspector)); expect($typehint)->toBeA('string'); expect($typehint)->toBe('array'); $inspector = Inspector::parameters($this->class, 'callableTypeHint'); $typehint = Inspector::typehint(current($inspector)); expect($typehint)->toBeA('string'); expect($typehint)->toBe('callable'); }); }); });
expect(Inspector::typehint($inspector[0]))->toBe(''); }); it("returns parameter typehint", function () { $inspector = Inspector::parameters($this->class, 'exceptionTypeHint'); $typehint = Inspector::typehint(current($inspector)); expect($typehint)->toBeA('string'); expect($typehint)->toBe('\\Exception'); $inspector = Inspector::parameters($this->class, 'arrayTypeHint'); $typehint = Inspector::typehint(current($inspector)); expect($typehint)->toBeA('string'); expect($typehint)->toBe('array'); $inspector = Inspector::parameters($this->class, 'callableTypeHint'); $typehint = Inspector::typehint(current($inspector)); expect($typehint)->toBeA('string'); expect($typehint)->toBe('callable'); }); it("returns parameter typehint for scalar type hints", function () { $typehint = Inspector::typehint(new Parameter('Parameter #0 [ <required> integer $values ]')); expect($typehint)->toBeA('string'); expect($typehint)->toBe('int'); $typehint = Inspector::typehint(new Parameter('Parameter #0 [ <required> boolean $values ]')); expect($typehint)->toBeA('string'); expect($typehint)->toBe('bool'); }); it("returns empty typehint for HHVM `mixed` type hint", function () { $typehint = Inspector::typehint(new Parameter('Parameter #0 [ <required> mixed $values ]')); expect($typehint)->toBeA('string'); expect($typehint)->toBe(''); }); }); });