/** * @param ParameterReflection $reflectionParameter * @return ParameterGenerator */ public static function fromReflection(ParameterReflection $reflectionParameter) { $param = new ParameterGenerator(); $param->setName($reflectionParameter->getName()); if ($reflectionParameter->isArray()) { $param->setType('array'); } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) { $param->setType('callable'); } else { $typeClass = $reflectionParameter->getClass(); if ($typeClass) { $parameterType = $typeClass->getName(); $currentNamespace = $reflectionParameter->getDeclaringClass()->getNamespaceName(); if (!empty($currentNamespace) && substr($parameterType, 0, strlen($currentNamespace)) == $currentNamespace) { $parameterType = substr($parameterType, strlen($currentNamespace) + 1); } else { $parameterType = '\\' . trim($parameterType, '\\'); } $param->setType($parameterType); } } $param->setPosition($reflectionParameter->getPosition()); if ($reflectionParameter->isOptional()) { $param->setDefaultValue($reflectionParameter->getDefaultValue()); } $param->setPassedByReference($reflectionParameter->isPassedByReference()); return $param; }
/** * @param ParameterReflection $reflectionParameter * @return ParameterGenerator */ public static function fromReflection(ParameterReflection $reflectionParameter) { $param = new ParameterGenerator(); $param->setName($reflectionParameter->getName()); if ($reflectionParameter->isArray()) { $param->setType('array'); } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) { $param->setType('callable'); } else { $typeClass = $reflectionParameter->getClass(); if ($typeClass) { $param->setType($typeClass->getName()); } } $param->setPosition($reflectionParameter->getPosition()); if ($reflectionParameter->isOptional()) { $param->setDefaultValue($reflectionParameter->getDefaultValue()); } $param->setPassedByReference($reflectionParameter->isPassedByReference()); return $param; }
/** * @param ParameterReflection $reflectionParameter * @return ParameterGenerator */ public static function fromReflection(ParameterReflection $reflectionParameter) { $param = new ParameterGenerator(); $param->setName($reflectionParameter->getName()); if ($type = self::extractFQCNTypeFromReflectionType($reflectionParameter)) { $param->setType($type); } $param->setPosition($reflectionParameter->getPosition()); $variadic = method_exists($reflectionParameter, 'isVariadic') && $reflectionParameter->isVariadic(); $param->setVariadic($variadic); if (!$variadic && $reflectionParameter->isOptional()) { try { $param->setDefaultValue($reflectionParameter->getDefaultValue()); } catch (\ReflectionException $e) { $param->setDefaultValue(null); } } $param->setPassedByReference($reflectionParameter->isPassedByReference()); return $param; }
private function buildClass($replacement) { $type = $this->splitNsandClass($replacement['originalFullyQualifiedType']); $class = new ClassGenerator(); $class->setName($type['class']); $class->setNamespaceName($type['ns']); $class->setExtendedClass('\\Brads\\Ppm\\Proxy'); $properties = []; $methods = []; $implementedInterfaces = []; foreach ($versions as $version => $info) { foreach ($info['files'] as $file) { echo "Parsing: " . $this->vendorDir . '/' . $package . '/' . $version . '/' . $file . "\n"; $parsedFile = new ReflectionFile($this->vendorDir . '/' . $package . '/' . $version . '/' . $file); $parsedClass = $parsedFile->getFileNamespace($info['toNs'])->getClass($info['toNs'] . '\\' . $type['class']); if ($parsedClass->getInterfaceNames() != null) { $implementedInterfaces = array_merge($implementedInterfaces, $parsedClass->getInterfaceNames()); } foreach ($parsedClass->getMethods() as $method) { if ($method->isPublic()) { $generatedMethod = new MethodGenerator(); $generatedMethod->setName($method->name); $generatedMethod->setBody('echo "Hello world!";'); $generatedMethod->setAbstract($method->isAbstract()); $generatedMethod->setFinal($method->isFinal()); $generatedMethod->setStatic($method->isStatic()); $generatedMethod->setVisibility(MethodGenerator::VISIBILITY_PUBLIC); foreach ($method->getParameters() as $param) { $generatedParam = new ParameterGenerator(); $generatedParam->setName($param->name); if ($param->hasType()) { $generatedParam->setType($param->getType()); } //$generatedParam->setDefaultValue($param->getDefaultValue()); $generatedParam->setPosition($param->getPosition()); $generatedParam->setVariadic($param->isVariadic()); $generatedParam->setPassedByReference($param->isPassedByReference()); $generatedMethod->setParameter($generatedParam); } $existingMethod = Linq::from($methods)->firstOrDefault(null, function (MethodGenerator $v) use($method) { return $v->getName() == $method->name; }); if ($existingMethod != null) { $existingParams = $existingMethod->getParameters(); foreach ($generatedMethod->getParameters() as $newParam) { $existingParam = Linq::from($existingParams)->firstOrDefault(function (ParameterGenerator $v) { return $v->getName() == $newParam->getName(); }); if ($existingParam == null) { $existingMethod->setParameter($newParam); } } } else { $methods[] = $generatedMethod; } } } foreach ($parsedClass->getProperties() as $prop) { //$properties[] = PropertyGenerator::fromReflection($prop); } } } $class->setImplementedInterfaces($implementedInterfaces); $class->addMethods($methods); $class->addProperties($properties); return (new FileGenerator(['classes' => [$class]]))->generate(); }
public function testPositionGetterAndSetterPersistValue() { $parameterGenerator = new ParameterGenerator(); $parameterGenerator->setPosition(2); $this->assertEquals(2, $parameterGenerator->getPosition()); }
/** * Generate method * * @param string $methodName * @return void */ protected function generateMethod($methodName) { $methodReflection = $this->_method[$methodName]; $docBlock = new DocBlockGenerator(); $docBlock->setShortDescription("Delicate {$methodName}() to __call() method "); if ($methodReflection->getDocComment()) { $docBlockReflection = new DocBlockReflection($methodReflection); $docBlock->fromReflection($docBlockReflection); } $method = new MethodGenerator(); $method->setName($methodName); $method->setDocBlock($docBlock); $method->setBody(sprintf(self::METHOD_TEMPLATE, $methodName)); if ($methodReflection->isPublic()) { $method->setVisibility(MethodGenerator::VISIBILITY_PUBLIC); } else { if ($methodReflection->isProtected()) { $method->setVisibility(MethodGenerator::VISIBILITY_PROTECTED); } else { if ($methodReflection->isPrivate()) { $method->setVisibility(MethodGenerator::VISIBILITY_PRIVATE); } } } foreach ($methodReflection->getParameters() as $parameter) { $parameterGenerator = new ParameterGenerator(); $parameterGenerator->setPosition($parameter->getPosition()); $parameterGenerator->setName($parameter->getName()); $parameterGenerator->setPassedByReference($parameter->isPassedByReference()); if ($parameter->isDefaultValueAvailable()) { $parameterGenerator->setDefaultValue($parameter->getDefaultValue()); } if ($parameter->isArray()) { $parameterGenerator->setType('array'); } if ($typeClass = $parameter->getClass()) { $parameterGenerator->setType($typeClass->getName()); } $method->setParameter($parameterGenerator); } $this->addMethodFromGenerator($method); }