/** * @test */ public function getParametersReturnsFlowsParameterReflection($dummyArg1 = null, $dummyArg2 = null) { $method = new Reflection\MethodReflection(__CLASS__, __FUNCTION__); foreach ($method->getParameters() as $parameter) { $this->assertInstanceOf(Reflection\ParameterReflection::class, $parameter); $this->assertEquals(__CLASS__, $parameter->getDeclaringClass()->getName()); } }
/** * @test */ public function getArgumentDefinitionsReturnsArrayOfArgumentDefinitionIfCommandExpectsArguments() { $parameterReflection = $this->createMock(ParameterReflection::class, [], [[__CLASS__, 'dummyMethod'], 'arg']); $mockReflectionService = $this->createMock(ReflectionService::class); $mockMethodParameters = ['argument1' => ['optional' => false], 'argument2' => ['optional' => true]]; $mockReflectionService->expects($this->atLeastOnce())->method('getMethodParameters')->will($this->returnValue($mockMethodParameters)); $this->command->injectReflectionService($mockReflectionService); $this->methodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue([$parameterReflection])); $this->methodReflection->expects($this->atLeastOnce())->method('getTagsValues')->will($this->returnValue(['param' => ['@param $argument1 argument1 description', '@param $argument2 argument2 description']])); $expectedResult = [new Cli\CommandArgumentDefinition('argument1', true, 'argument1 description'), new Cli\CommandArgumentDefinition('argument2', false, 'argument2 description')]; $actualResult = $this->command->getArgumentDefinitions(); $this->assertEquals($expectedResult, $actualResult); }
/** * Compile the result of methods marked with CompileStatic into the proxy class * * @param string $className * @param ProxyClass $proxyClass * @return void * @throws ObjectException */ protected function compileStaticMethods($className, ProxyClass $proxyClass) { $methodNames = $this->reflectionService->getMethodsAnnotatedWith($className, Flow\CompileStatic::class); foreach ($methodNames as $methodName) { if (!$this->reflectionService->isMethodStatic($className, $methodName)) { throw new ObjectException(sprintf('The method %s:%s() is annotated CompileStatic so it must be static', $className, $methodName), 1476348303); } if ($this->reflectionService->isMethodPrivate($className, $methodName)) { throw new ObjectException(sprintf('The method %s:%s() is annotated CompileStatic so it must not be private', $className, $methodName), 1476348306); } $reflectedMethod = new MethodReflection($className, $methodName); $reflectedMethod->setAccessible(true); $value = $reflectedMethod->invoke(null, $this->objectManager); $compiledResult = var_export($value, true); $compiledMethod = $proxyClass->getMethod($methodName); $compiledMethod->setMethodBody('return ' . $compiledResult . ';'); } }
/** * Converts the given parameter reflection into an information array * * @param ParameterReflection $parameter The parameter to reflect * @param MethodReflection $method The parameter's method * @return array Parameter information array */ protected function convertParameterReflectionToArray(ParameterReflection $parameter, MethodReflection $method = null) { $parameterInformation = [self::DATA_PARAMETER_POSITION => $parameter->getPosition()]; if ($parameter->isPassedByReference()) { $parameterInformation[self::DATA_PARAMETER_BY_REFERENCE] = true; } if ($parameter->isArray()) { $parameterInformation[self::DATA_PARAMETER_ARRAY] = true; } if ($parameter->isOptional()) { $parameterInformation[self::DATA_PARAMETER_OPTIONAL] = true; } if ($parameter->allowsNull()) { $parameterInformation[self::DATA_PARAMETER_ALLOWS_NULL] = true; } $parameterClass = $parameter->getClass(); if ($parameterClass !== null) { $parameterInformation[self::DATA_PARAMETER_CLASS] = $parameterClass->getName(); } if ($parameter->isOptional() && $parameter->isDefaultValueAvailable()) { $parameterInformation[self::DATA_PARAMETER_DEFAULT_VALUE] = $parameter->getDefaultValue(); } if ($method !== null) { $paramAnnotations = $method->isTaggedWith('param') ? $method->getTagValues('param') : []; if (isset($paramAnnotations[$parameter->getPosition()])) { $explodedParameters = explode(' ', $paramAnnotations[$parameter->getPosition()]); if (count($explodedParameters) >= 2) { $parameterType = $this->expandType($method->getDeclaringClass(), $explodedParameters[0]); $parameterInformation[self::DATA_PARAMETER_TYPE] = $this->cleanClassName($parameterType); } } if (!$parameter->isArray()) { $builtinType = $parameter->getBuiltinType(); if ($builtinType !== null) { $parameterInformation[self::DATA_PARAMETER_TYPE] = $builtinType; $parameterInformation[self::DATA_PARAMETER_SCALAR_DECLARATION] = true; } } } if (!isset($parameterInformation[self::DATA_PARAMETER_TYPE]) && $parameterClass !== null) { $parameterInformation[self::DATA_PARAMETER_TYPE] = $this->cleanClassName($parameterClass->getName()); } elseif (!isset($parameterInformation[self::DATA_PARAMETER_TYPE])) { $parameterInformation[self::DATA_PARAMETER_TYPE] = 'mixed'; } return $parameterInformation; }