diff --git a/src/Annotation/AnnotationCollection.php b/src/Annotation/AnnotationCollection.php index 74e2b050..3985bb2d 100644 --- a/src/Annotation/AnnotationCollection.php +++ b/src/Annotation/AnnotationCollection.php @@ -17,14 +17,11 @@ class AnnotationCollection extends ArrayObject { /** * Checks if the collection has annotations for a class - * - * @param string $class - * @return bool */ - public function hasAnnotation($class) + public function hasAnnotation(string $class) : bool { foreach ($this as $annotation) { - if (get_class($annotation) == $class) { + if (get_class($annotation) === $class) { return true; } } diff --git a/src/Annotation/AnnotationManager.php b/src/Annotation/AnnotationManager.php index 8272fe60..1da51f06 100644 --- a/src/Annotation/AnnotationManager.php +++ b/src/Annotation/AnnotationManager.php @@ -37,12 +37,9 @@ class AnnotationManager implements EventManagerAwareInterface protected $events; /** - * Set the event manager instance - * - * @param EventManagerInterface $events - * @return AnnotationManager + * {@inheritDoc} */ - public function setEventManager(EventManagerInterface $events) + public function setEventManager(EventManagerInterface $events) : self { $events->setIdentifiers([ __CLASS__, @@ -54,13 +51,9 @@ public function setEventManager(EventManagerInterface $events) } /** - * Retrieve event manager - * - * Lazy loads an instance if none registered. - * - * @return EventManagerInterface + * {@inheritDoc} */ - public function getEventManager() + public function getEventManager() : EventManagerInterface { if (null === $this->events) { $this->setEventManager(new EventManager()); @@ -71,11 +64,8 @@ public function getEventManager() /** * Attach a parser to listen to the createAnnotation event - * - * @param ParserInterface $parser - * @return AnnotationManager */ - public function attach(ParserInterface $parser) + public function attach(ParserInterface $parser) : self { $this->getEventManager() ->attach(self::EVENT_CREATE_ANNOTATION, [$parser, 'onCreateAnnotation']); diff --git a/src/Annotation/Parser/DoctrineAnnotationParser.php b/src/Annotation/Parser/DoctrineAnnotationParser.php index 64f7ffa5..3ee2c7a3 100644 --- a/src/Annotation/Parser/DoctrineAnnotationParser.php +++ b/src/Annotation/Parser/DoctrineAnnotationParser.php @@ -55,11 +55,8 @@ public function __construct() /** * Set the DocParser instance - * - * @param DocParser $docParser - * @return DoctrineAnnotationParser */ - public function setDocParser(DocParser $docParser) + public function setDocParser(DocParser $docParser) : self { $this->docParser = $docParser; return $this; @@ -69,10 +66,8 @@ public function setDocParser(DocParser $docParser) * Retrieve the DocParser instance * * If none is registered, lazy-loads a new instance. - * - * @return DocParser */ - public function getDocParser() + public function getDocParser() : DocParser { if (! $this->docParser instanceof DocParser) { $this->setDocParser(new DocParser()); @@ -127,9 +122,8 @@ public function onCreateAnnotation(EventInterface $e) * Specify an allowed annotation class * * @param string $annotation - * @return DoctrineAnnotationParser */ - public function registerAnnotation($annotation) + public function registerAnnotation($annotation) : self { $this->allowedAnnotations[$annotation] = true; return $this; @@ -141,9 +135,8 @@ public function registerAnnotation($annotation) * @param array|Traversable $annotations Array or traversable object of * annotation class names * @throws Exception\InvalidArgumentException - * @return DoctrineAnnotationParser */ - public function registerAnnotations($annotations) + public function registerAnnotations($annotations) : self { if (! is_array($annotations) && ! $annotations instanceof Traversable) { throw new Exception\InvalidArgumentException(sprintf( diff --git a/src/Annotation/Parser/GenericAnnotationParser.php b/src/Annotation/Parser/GenericAnnotationParser.php index 6515e775..895cfd04 100644 --- a/src/Annotation/Parser/GenericAnnotationParser.php +++ b/src/Annotation/Parser/GenericAnnotationParser.php @@ -93,10 +93,9 @@ public function onCreateAnnotation(EventInterface $e) * * @param string|AnnotationInterface $annotation String class name of an * AnnotationInterface implementation, or actual instance - * @return void * @throws Exception\InvalidArgumentException */ - public function registerAnnotation($annotation) + public function registerAnnotation($annotation) : void { $class = false; if (is_string($annotation) && class_exists($annotation)) { @@ -131,9 +130,8 @@ public function registerAnnotation($annotation) * * @param array|Traversable $annotations * @throws Exception\InvalidArgumentException - * @return GenericAnnotationParser */ - public function registerAnnotations($annotations) + public function registerAnnotations($annotations) : self { if (! is_array($annotations) && ! $annotations instanceof Traversable) { throw new Exception\InvalidArgumentException(sprintf( @@ -152,11 +150,8 @@ public function registerAnnotations($annotations) /** * Checks if the manager has annotations for a class - * - * @param string $class - * @return bool */ - public function hasAnnotation($class) + public function hasAnnotation(string $class) : bool { if (in_array($class, $this->annotationNames)) { return true; @@ -175,9 +170,8 @@ public function hasAnnotation($class) * @param string $alias * @param string $class May be either a registered annotation name or another alias * @throws Exception\InvalidArgumentException - * @return GenericAnnotationParser */ - public function setAlias($alias, $class) + public function setAlias(string $alias, string $class) : self { if (! in_array($class, $this->annotationNames) && ! $this->hasAlias($class)) { throw new Exception\InvalidArgumentException(sprintf( @@ -195,24 +189,15 @@ public function setAlias($alias, $class) return $this; } - /** - * Normalize an alias name - * - * @param string $alias - * @return string - */ - protected function normalizeAlias($alias) + protected function normalizeAlias(string $alias) : string { return strtolower(str_replace(['-', '_', ' ', '\\', '/'], '', $alias)); } /** * Do we have an alias by the provided name? - * - * @param string $alias - * @return bool */ - protected function hasAlias($alias) + protected function hasAlias(string $alias) : bool { $alias = $this->normalizeAlias($alias); @@ -221,11 +206,8 @@ protected function hasAlias($alias) /** * Resolve an alias to a class name - * - * @param string $alias - * @return string */ - protected function resolveAlias($alias) + protected function resolveAlias(string $alias) : string { do { $normalized = $this->normalizeAlias($alias); diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index c48ce091..d9a17b2c 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -9,6 +9,6 @@ namespace Zend\Code\Exception; -interface ExceptionInterface +interface ExceptionInterface extends \Throwable { } diff --git a/src/Generator/AbstractGenerator.php b/src/Generator/AbstractGenerator.php index 68087ce6..3660cf0f 100644 --- a/src/Generator/AbstractGenerator.php +++ b/src/Generator/AbstractGenerator.php @@ -50,66 +50,43 @@ public function __construct($options = []) } } - /** - * @param bool $isSourceDirty - * @return AbstractGenerator - */ - public function setSourceDirty($isSourceDirty = true) + public function setSourceDirty(bool $isSourceDirty = true) : self { - $this->isSourceDirty = (bool) $isSourceDirty; + $this->isSourceDirty = $isSourceDirty; return $this; } - /** - * @return bool - */ - public function isSourceDirty() + public function isSourceDirty() : bool { return $this->isSourceDirty; } - /** - * @param string $indentation - * @return AbstractGenerator - */ - public function setIndentation($indentation) + public function setIndentation(string $indentation) : self { - $this->indentation = (string) $indentation; + $this->indentation = $indentation; return $this; } - /** - * @return string - */ - public function getIndentation() + public function getIndentation() : string { return $this->indentation; } - /** - * @param string $sourceContent - * @return AbstractGenerator - */ - public function setSourceContent($sourceContent) + public function setSourceContent(?string $sourceContent) : self { - $this->sourceContent = (string) $sourceContent; + $this->sourceContent = $sourceContent; return $this; } - /** - * @return string - */ - public function getSourceContent() + public function getSourceContent() : ?string { return $this->sourceContent; } /** - * @param array|Traversable $options * @throws Exception\InvalidArgumentException - * @return AbstractGenerator */ - public function setOptions($options) + public function setOptions(iterable $options) : self { if (! is_array($options) && ! $options instanceof Traversable) { throw new Exception\InvalidArgumentException(sprintf( diff --git a/src/Generator/AbstractMemberGenerator.php b/src/Generator/AbstractMemberGenerator.php index b332da9b..56d46af3 100644 --- a/src/Generator/AbstractMemberGenerator.php +++ b/src/Generator/AbstractMemberGenerator.php @@ -52,116 +52,75 @@ abstract class AbstractMemberGenerator extends AbstractGenerator /** * @param int|array $flags - * @return AbstractMemberGenerator */ - public function setFlags($flags) - { - if (is_array($flags)) { - $flagsArray = $flags; - $flags = 0x00; - foreach ($flagsArray as $flag) { - $flags |= $flag; - } + public function setFlags($flags) : self + { + if (! is_array($flags)) { + $this->flags = $flags; + + return $this; } - // check that visibility is one of three - $this->flags = $flags; + + $this->flags = array_reduce($flags, function ($previousFlags, $flag) { + return $previousFlags | $flag; + }, 0x00); return $this; } - /** - * @param int $flag - * @return AbstractMemberGenerator - */ - public function addFlag($flag) + public function addFlag(int $flag) : self { $this->setFlags($this->flags | $flag); return $this; } - /** - * @param int $flag - * @return AbstractMemberGenerator - */ - public function removeFlag($flag) + public function removeFlag(int $flag) : self { $this->setFlags($this->flags & ~$flag); return $this; } - /** - * @param bool $isAbstract - * @return AbstractMemberGenerator - */ - public function setAbstract($isAbstract) + public function setAbstract(bool $isAbstract) : self { return $isAbstract ? $this->addFlag(self::FLAG_ABSTRACT) : $this->removeFlag(self::FLAG_ABSTRACT); } - /** - * @return bool - */ - public function isAbstract() + public function isAbstract() : bool { return (bool) ($this->flags & self::FLAG_ABSTRACT); } - /** - * @param bool $isInterface - * @return AbstractMemberGenerator - */ - public function setInterface($isInterface) + public function setInterface(bool $isInterface) : self { return $isInterface ? $this->addFlag(self::FLAG_INTERFACE) : $this->removeFlag(self::FLAG_INTERFACE); } - /** - * @return bool - */ - public function isInterface() + public function isInterface() : bool { return (bool) ($this->flags & self::FLAG_INTERFACE); } - /** - * @param bool $isFinal - * @return AbstractMemberGenerator - */ - public function setFinal($isFinal) + public function setFinal(bool $isFinal) : self { return $isFinal ? $this->addFlag(self::FLAG_FINAL) : $this->removeFlag(self::FLAG_FINAL); } - /** - * @return bool - */ - public function isFinal() + public function isFinal() : bool { return (bool) ($this->flags & self::FLAG_FINAL); } - /** - * @param bool $isStatic - * @return AbstractMemberGenerator - */ - public function setStatic($isStatic) + public function setStatic(bool $isStatic) : self { return $isStatic ? $this->addFlag(self::FLAG_STATIC) : $this->removeFlag(self::FLAG_STATIC); } - /** - * @return bool - */ - public function isStatic() + public function isStatic() : bool { return (bool) ($this->flags & self::FLAG_STATIC); // is FLAG_STATIC in flags } - /** - * @param string $visibility - * @return AbstractMemberGenerator - */ - public function setVisibility($visibility) + public function setVisibility(string $visibility) : self { switch ($visibility) { case self::VISIBILITY_PUBLIC: @@ -181,10 +140,7 @@ public function setVisibility($visibility) return $this; } - /** - * @return string - */ - public function getVisibility() + public function getVisibility() : string { switch (true) { case $this->flags & self::FLAG_PROTECTED: @@ -196,20 +152,13 @@ public function getVisibility() } } - /** - * @param string $name - * @return AbstractMemberGenerator - */ - public function setName($name) + public function setName(string $name) : self { - $this->name = (string) $name; + $this->name = $name; return $this; } - /** - * @return string - */ - public function getName() + public function getName() : ?string { return $this->name; } @@ -217,9 +166,8 @@ public function getName() /** * @param DocBlockGenerator|string $docBlock * @throws Exception\InvalidArgumentException - * @return AbstractMemberGenerator */ - public function setDocBlock($docBlock) + public function setDocBlock($docBlock) : self { if (is_string($docBlock)) { $docBlock = new DocBlockGenerator($docBlock); @@ -236,10 +184,7 @@ public function setDocBlock($docBlock) return $this; } - /** - * @return DocBlockGenerator - */ - public function getDocBlock() + public function getDocBlock() : ?DocBlockGenerator { return $this->docBlock; } diff --git a/src/Generator/BodyGenerator.php b/src/Generator/BodyGenerator.php index aecd799f..112f1da2 100644 --- a/src/Generator/BodyGenerator.php +++ b/src/Generator/BodyGenerator.php @@ -12,32 +12,22 @@ class BodyGenerator extends AbstractGenerator { /** - * @var string + * @var string|null */ protected $content; - /** - * @param string $content - * @return BodyGenerator - */ - public function setContent($content) + public function setContent(string $content) : self { $this->content = (string) $content; return $this; } - /** - * @return string - */ - public function getContent() + public function getContent() : ?string { return $this->content; } - /** - * @return string - */ - public function generate() + public function generate() : ?string { return $this->getContent(); } diff --git a/src/Generator/ClassGenerator.php b/src/Generator/ClassGenerator.php index 2db218c8..f1f00642 100644 --- a/src/Generator/ClassGenerator.php +++ b/src/Generator/ClassGenerator.php @@ -98,11 +98,8 @@ class ClassGenerator extends AbstractGenerator implements TraitUsageInterface /** * Build a Code Generation Php Object from a Class Reflection - * - * @param ClassReflection $classReflection - * @return self */ - public static function fromReflection(ClassReflection $classReflection) + public static function fromReflection(ClassReflection $classReflection) : self { $cg = new static($classReflection->getName()); @@ -188,10 +185,8 @@ public static function fromReflection(ClassReflection $classReflection) * @configkey methods * * @throws Exception\InvalidArgumentException - * @param array $array - * @return self */ - public static function fromArray(array $array) + public static function fromArray(array $array) : self { if (! isset($array['name'])) { throw new Exception\InvalidArgumentException( @@ -282,15 +277,11 @@ public function __construct( } } - /** - * @param string $name - * @return self - */ - public function setName($name) + public function setName(string $name) : self { - if (strstr($name, '\\')) { + if (false !== strpos($name, '\\')) { $namespace = substr($name, 0, strrpos($name, '\\')); - $name = substr($name, strrpos($name, '\\') + 1); + $name = (string) substr($name, strrpos($name, '\\') + 1); $this->setNamespaceName($namespace); } @@ -298,92 +289,68 @@ public function setName($name) return $this; } - /** - * @return string - */ - public function getName() + public function getName() : string { return $this->name; } - /** - * @param string $namespaceName - * @return self - */ - public function setNamespaceName($namespaceName) + public function setNamespaceName(?string $namespaceName) : self { $this->namespaceName = $namespaceName; return $this; } - /** - * @return string - */ - public function getNamespaceName() + public function getNamespaceName() : ?string { return $this->namespaceName; } - /** - * @param FileGenerator $fileGenerator - * @return self - */ - public function setContainingFileGenerator(FileGenerator $fileGenerator) + public function setContainingFileGenerator(FileGenerator $fileGenerator) : self { $this->containingFileGenerator = $fileGenerator; return $this; } - /** - * @return FileGenerator - */ - public function getContainingFileGenerator() + public function getContainingFileGenerator() : ?FileGenerator { return $this->containingFileGenerator; } - /** - * @param DocBlockGenerator $docBlock - * @return self - */ - public function setDocBlock(DocBlockGenerator $docBlock) + public function setDocBlock(DocBlockGenerator $docBlock) : self { $this->docBlock = $docBlock; return $this; } - /** - * @return DocBlockGenerator - */ - public function getDocBlock() + public function getDocBlock() : ?DocBlockGenerator { return $this->docBlock; } /** * @param array|string $flags - * @return self */ - public function setFlags($flags) + public function setFlags($flags) : self { - if (is_array($flags)) { - $flagsArray = $flags; - $flags = 0x00; - foreach ($flagsArray as $flag) { - $flags |= $flag; - } + if (! is_array($flags)) { + + // check that visibility is one of three + $this->flags = $flags; + + return $this; } - // check that visibility is one of three - $this->flags = $flags; + + $this->flags = array_reduce($flags, function ($carry, $nextFlag) { + return $carry | $nextFlag; + }, 0x00); return $this; } /** * @param string $flag - * @return self */ - public function addFlag($flag) + public function addFlag($flag) : self { $this->setFlags($this->flags | $flag); return $this; @@ -391,88 +358,56 @@ public function addFlag($flag) /** * @param string $flag - * @return self */ - public function removeFlag($flag) + public function removeFlag($flag) : self { $this->setFlags($this->flags & ~$flag); return $this; } - /** - * @param bool $isAbstract - * @return self - */ - public function setAbstract($isAbstract) + public function setAbstract(bool $isAbstract) : self { return $isAbstract ? $this->addFlag(self::FLAG_ABSTRACT) : $this->removeFlag(self::FLAG_ABSTRACT); } - /** - * @return bool - */ - public function isAbstract() + public function isAbstract() : bool { return (bool) ($this->flags & self::FLAG_ABSTRACT); } - /** - * @param bool $isFinal - * @return self - */ - public function setFinal($isFinal) + public function setFinal(bool $isFinal) : self { return $isFinal ? $this->addFlag(self::FLAG_FINAL) : $this->removeFlag(self::FLAG_FINAL); } - /** - * @return bool - */ - public function isFinal() + public function isFinal() : bool { return $this->flags & self::FLAG_FINAL; } - /** - * @param string $extendedClass - * @return self - */ - public function setExtendedClass($extendedClass) + public function setExtendedClass(?string $extendedClass) : self { $this->extendedClass = $extendedClass; return $this; } - /** - * @return string - */ - public function getExtendedClass() + public function getExtendedClass() : ?string { return $this->extendedClass; } - /** - * @return bool - */ - public function hasExtentedClass() + public function hasExtentedClass() : bool { return ! empty($this->extendedClass); } - /** - * @return self - */ - public function removeExtentedClass() + public function removeExtentedClass() : self { $this->setExtendedClass(null); return $this; } - /** - * @param array $implementedInterfaces - * @return self - */ - public function setImplementedInterfaces(array $implementedInterfaces) + public function setImplementedInterfaces(array $implementedInterfaces) : self { array_map(function ($implementedInterface) { return (string) TypeGenerator::fromTypeString($implementedInterface); @@ -482,29 +417,18 @@ public function setImplementedInterfaces(array $implementedInterfaces) return $this; } - /** - * @return array - */ - public function getImplementedInterfaces() + public function getImplementedInterfaces() : array { return $this->implementedInterfaces; } - /** - * @param string $implementedInterface - * @return bool - */ - public function hasImplementedInterface($implementedInterface) + public function hasImplementedInterface(string $implementedInterface) : bool { $implementedInterface = (string) TypeGenerator::fromTypeString($implementedInterface); return in_array($implementedInterface, $this->implementedInterfaces); } - /** - * @param string $implementedInterface - * @return self - */ - public function removeImplementedInterface($implementedInterface) + public function removeImplementedInterface(string $implementedInterface) : self { $implementedInterface = (string) TypeGenerator::fromTypeString($implementedInterface); unset($this->implementedInterfaces[array_search($implementedInterface, $this->implementedInterfaces)]); @@ -512,10 +436,9 @@ public function removeImplementedInterface($implementedInterface) } /** - * @param string $constantName - * @return PropertyGenerator|false + * @return PropertyGenerator|bool */ - public function getConstant($constantName) + public function getConstant(string $constantName) { if (isset($this->constants[$constantName])) { return $this->constants[$constantName]; @@ -527,27 +450,19 @@ public function getConstant($constantName) /** * @return PropertyGenerator[] indexed by constant name */ - public function getConstants() + public function getConstants() : array { return $this->constants; } - /** - * @param string $constantName - * @return self - */ - public function removeConstant($constantName) + public function removeConstant(string $constantName) : self { unset($this->constants[$constantName]); return $this; } - /** - * @param string $constantName - * @return bool - */ - public function hasConstant($constantName) + public function hasConstant(string $constantName) : bool { return isset($this->constants[$constantName]); } @@ -555,11 +470,9 @@ public function hasConstant($constantName) /** * Add constant from PropertyGenerator * - * @param PropertyGenerator $constant * @throws Exception\InvalidArgumentException - * @return self */ - public function addConstantFromGenerator(PropertyGenerator $constant) + public function addConstantFromGenerator(PropertyGenerator $constant) : self { $constantName = $constant->getName(); @@ -589,10 +502,8 @@ public function addConstantFromGenerator(PropertyGenerator $constant) * @param string|int|null|float|array $value Scalar * * @throws Exception\InvalidArgumentException - * - * @return self */ - public function addConstant($name, $value) + public function addConstant(string $name, $value) : self { if (empty($name) || ! is_string($name)) { throw new Exception\InvalidArgumentException(sprintf( @@ -610,10 +521,8 @@ public function addConstant($name, $value) /** * @param PropertyGenerator[]|array[] $constants - * - * @return self */ - public function addConstants(array $constants) + public function addConstants(array $constants) : self { foreach ($constants as $constant) { if ($constant instanceof PropertyGenerator) { @@ -628,11 +537,7 @@ public function addConstants(array $constants) return $this; } - /** - * @param array $properties - * @return self - */ - public function addProperties(array $properties) + public function addProperties(array $properties) : self { foreach ($properties as $property) { if ($property instanceof PropertyGenerator) { @@ -656,18 +561,9 @@ public function addProperties(array $properties) * @param string|array $defaultValue * @param int $flags * @throws Exception\InvalidArgumentException - * @return self */ - public function addProperty($name, $defaultValue = null, $flags = PropertyGenerator::FLAG_PUBLIC) + public function addProperty(string $name, $defaultValue = null, int $flags = PropertyGenerator::FLAG_PUBLIC) : self { - if (! is_string($name)) { - throw new Exception\InvalidArgumentException(sprintf( - '%s::%s expects string for name', - get_class($this), - __FUNCTION__ - )); - } - // backwards compatibility // @todo remove this on next major version if ($flags === PropertyGenerator::FLAG_CONSTANT) { @@ -680,11 +576,9 @@ public function addProperty($name, $defaultValue = null, $flags = PropertyGenera /** * Add property from PropertyGenerator * - * @param PropertyGenerator $property * @throws Exception\InvalidArgumentException - * @return self */ - public function addPropertyFromGenerator(PropertyGenerator $property) + public function addPropertyFromGenerator(PropertyGenerator $property) : self { $propertyName = $property->getName(); @@ -708,16 +602,16 @@ public function addPropertyFromGenerator(PropertyGenerator $property) /** * @return PropertyGenerator[] */ - public function getProperties() + public function getProperties() : array { return $this->properties; } /** * @param string $propertyName - * @return PropertyGenerator|false + * @return PropertyGenerator|bool */ - public function getProperty($propertyName) + public function getProperty(string $propertyName) { foreach ($this->getProperties() as $property) { if ($property->getName() == $propertyName) { @@ -730,50 +624,30 @@ public function getProperty($propertyName) /** * Add a class to "use" classes - * - * @param string $use - * @param string|null $useAlias - * @return self */ - public function addUse($use, $useAlias = null) + public function addUse($use, $useAlias = null) : self { $this->traitUsageGenerator->addUse($use, $useAlias); return $this; } - /** - * @param string $use - * @return self - */ - public function hasUse($use) + public function hasUse(string $use) : bool { return $this->traitUsageGenerator->hasUse($use); } - /** - * @param string $use - * @return self - */ - public function removeUse($use) + public function removeUse(string $use) : self { $this->traitUsageGenerator->removeUse($use); return $this; } - /** - * @param string $use - * @return bool - */ - public function hasUseAlias($use) + public function hasUseAlias(string $use) : bool { return $this->traitUsageGenerator->hasUseAlias($use); } - /** - * @param string $use - * @return self - */ - public function removeUseAlias($use) + public function removeUseAlias(string $use) : self { $this->traitUsageGenerator->removeUseAlias($use); return $this; @@ -781,39 +655,25 @@ public function removeUseAlias($use) /** * Returns the "use" classes - * - * @return array */ - public function getUses() + public function getUses() : array { return $this->traitUsageGenerator->getUses(); } - /** - * @param string $propertyName - * @return self - */ - public function removeProperty($propertyName) + public function removeProperty(string $propertyName) : self { unset($this->properties[$propertyName]); return $this; } - /** - * @param string $propertyName - * @return bool - */ - public function hasProperty($propertyName) + public function hasProperty(string $propertyName) : bool { return isset($this->properties[$propertyName]); } - /** - * @param array $methods - * @return self - */ - public function addMethods(array $methods) + public function addMethods(array $methods) : self { foreach ($methods as $method) { if ($method instanceof MethodGenerator) { @@ -833,21 +693,15 @@ public function addMethods(array $methods) /** * Add Method from scalars * - * @param string $name - * @param array $parameters - * @param int $flags - * @param string $body - * @param string $docBlock * @throws Exception\InvalidArgumentException - * @return self */ public function addMethod( - $name, + string $name, array $parameters = [], - $flags = MethodGenerator::FLAG_PUBLIC, - $body = null, - $docBlock = null - ) { + int $flags = MethodGenerator::FLAG_PUBLIC, + ?string $body = null, + ?string $docBlock = null + ) : self { if (! is_string($name)) { throw new Exception\InvalidArgumentException(sprintf( '%s::%s expects string for name', @@ -862,11 +716,9 @@ public function addMethod( /** * Add Method from MethodGenerator * - * @param MethodGenerator $method * @throws Exception\InvalidArgumentException - * @return self */ - public function addMethodFromGenerator(MethodGenerator $method) + public function addMethodFromGenerator(MethodGenerator $method) : self { $methodName = $method->getName(); @@ -884,36 +736,27 @@ public function addMethodFromGenerator(MethodGenerator $method) /** * @return MethodGenerator[] */ - public function getMethods() + public function getMethods() : array { return $this->methods; } /** - * @param string $methodName - * @return MethodGenerator|false + * @return MethodGenerator|bool */ - public function getMethod($methodName) + public function getMethod(string $methodName) { return $this->hasMethod($methodName) ? $this->methods[strtolower($methodName)] : false; } - /** - * @param string $methodName - * @return self - */ - public function removeMethod($methodName) + public function removeMethod(string $methodName) : self { unset($this->methods[strtolower($methodName)]); return $this; } - /** - * @param string $methodName - * @return bool - */ - public function hasMethod($methodName) + public function hasMethod(string $methodName) : bool { return isset($this->methods[strtolower($methodName)]); } @@ -957,13 +800,15 @@ public function getTraits() */ public function removeTrait($traitName) { - return $this->traitUsageGenerator->removeTrait($traitName); + $this->traitUsageGenerator->removeTrait($traitName); + + return $this; } /** * @inheritDoc */ - public function addTraitAlias($method, $alias, $visibility = null) + public function addTraitAlias($method, $alias, $visibility = null) : self { $this->traitUsageGenerator->addTraitAlias($method, $alias, $visibility); return $this; @@ -972,7 +817,7 @@ public function addTraitAlias($method, $alias, $visibility = null) /** * @inheritDoc */ - public function getTraitAliases() + public function getTraitAliases() : array { return $this->traitUsageGenerator->getTraitAliases(); } @@ -980,7 +825,7 @@ public function getTraitAliases() /** * @inheritDoc */ - public function addTraitOverride($method, $traitsToReplace) + public function addTraitOverride($method, $traitsToReplace) : self { $this->traitUsageGenerator->addTraitOverride($method, $traitsToReplace); return $this; @@ -989,7 +834,7 @@ public function addTraitOverride($method, $traitsToReplace) /** * @inheritDoc */ - public function removeTraitOverride($method, $overridesToRemove = null) + public function removeTraitOverride($method, $overridesToRemove = null) : self { $this->traitUsageGenerator->removeTraitOverride($method, $overridesToRemove); @@ -999,7 +844,7 @@ public function removeTraitOverride($method, $overridesToRemove = null) /** * @inheritDoc */ - public function getTraitOverrides() + public function getTraitOverrides() : array { return $this->traitUsageGenerator->getTraitOverrides(); } @@ -1007,7 +852,7 @@ public function getTraitOverrides() /** * @return bool */ - public function isSourceDirty() + public function isSourceDirty() : bool { if (($docBlock = $this->getDocBlock()) && $docBlock->isSourceDirty()) { return true; @@ -1028,10 +873,7 @@ public function isSourceDirty() return parent::isSourceDirty(); } - /** - * @inheritDoc - */ - public function generate() + public function generate() : string { if (! $this->isSourceDirty()) { $output = $this->getSourceContent(); @@ -1040,7 +882,6 @@ public function generate() } } - $indent = $this->getIndentation(); $output = ''; if (null !== ($namespace = $this->getNamespaceName())) { @@ -1110,11 +951,9 @@ public function generate() /** * @param mixed $value * - * @return void - * * @throws Exception\InvalidArgumentException */ - private function validateConstantValue($value) + private function validateConstantValue($value) : void { if (null === $value || is_scalar($value)) { return; @@ -1132,12 +971,7 @@ private function validateConstantValue($value) )); } - /** - * @param string $fqnClassName - * - * @return string - */ - private function generateShortOrCompleteClassname($fqnClassName) + private function generateShortOrCompleteClassname(string $fqnClassName) : string { $fqnClassName = ltrim($fqnClassName, '\\'); $parts = explode('\\', $fqnClassName); diff --git a/src/Generator/DocBlock/Tag.php b/src/Generator/DocBlock/Tag.php index 1fb500bc..c109e1ca 100644 --- a/src/Generator/DocBlock/Tag.php +++ b/src/Generator/DocBlock/Tag.php @@ -18,11 +18,9 @@ class Tag extends GenericTag { /** - * @param ReflectionTagInterface $reflectionTag - * @return Tag * @deprecated Deprecated in 2.3. Use TagManager::createTagFromReflection() instead */ - public static function fromReflection(ReflectionTagInterface $reflectionTag) + public static function fromReflection(ReflectionTagInterface $reflectionTag) : self { $tagManager = new TagManager(); $tagManager->initializeDefaultTags(); @@ -30,20 +28,17 @@ public static function fromReflection(ReflectionTagInterface $reflectionTag) } /** - * @param string $description - * @return Tag * @deprecated Deprecated in 2.3. Use GenericTag::setContent() instead */ - public function setDescription($description) + public function setDescription(string $description) : self { return $this->setContent($description); } /** - * @return string * @deprecated Deprecated in 2.3. Use GenericTag::getContent() instead */ - public function getDescription() + public function getDescription() : ?string { return $this->getContent(); } diff --git a/src/Generator/DocBlock/Tag/AbstractTypeableTag.php b/src/Generator/DocBlock/Tag/AbstractTypeableTag.php index 0e47a7ce..342629cd 100644 --- a/src/Generator/DocBlock/Tag/AbstractTypeableTag.php +++ b/src/Generator/DocBlock/Tag/AbstractTypeableTag.php @@ -48,20 +48,13 @@ public function __construct($types = [], $description = null) } } - /** - * @param string $description - * @return ReturnTag - */ - public function setDescription($description) + public function setDescription(?string $description) : self { $this->description = $description; return $this; } - /** - * @return string - */ - public function getDescription() + public function getDescription() : ?string { return $this->description; } @@ -70,10 +63,9 @@ public function getDescription() * Array of types or string with types delimited by pipe (|) * e.g. array('int', 'null') or "int|null" * - * @param array|string $types - * @return ReturnTag + * @param string[]|string $types */ - public function setTypes($types) + public function setTypes($types) : self { if (is_string($types)) { $types = explode('|', $types); @@ -82,19 +74,12 @@ public function setTypes($types) return $this; } - /** - * @return array - */ - public function getTypes() + public function getTypes() : array { return $this->types; } - /** - * @param string $delimiter - * @return string - */ - public function getTypesAsString($delimiter = '|') + public function getTypesAsString(string $delimiter = '|') : string { return implode($delimiter, $this->types); } diff --git a/src/Generator/DocBlock/Tag/AuthorTag.php b/src/Generator/DocBlock/Tag/AuthorTag.php index b4bd916d..3e568840 100644 --- a/src/Generator/DocBlock/Tag/AuthorTag.php +++ b/src/Generator/DocBlock/Tag/AuthorTag.php @@ -41,70 +41,46 @@ public function __construct($authorName = null, $authorEmail = null) } /** - * @param ReflectionTagInterface $reflectionTag - * @return ReturnTag * @deprecated Deprecated in 2.3. Use TagManager::createTagFromReflection() instead */ - public static function fromReflection(ReflectionTagInterface $reflectionTag) + public static function fromReflection(ReflectionTagInterface $reflectionTag) : self { $tagManager = new TagManager(); $tagManager->initializeDefaultTags(); return $tagManager->createTagFromReflection($reflectionTag); } - /** - * @return string - */ - public function getName() + public function getName() : string { return 'author'; } - /** - * @param string $authorEmail - * @return AuthorTag - */ - public function setAuthorEmail($authorEmail) + public function setAuthorEmail(string $authorEmail) : self { $this->authorEmail = $authorEmail; return $this; } - /** - * @return string - */ - public function getAuthorEmail() + public function getAuthorEmail() : ?string { return $this->authorEmail; } - /** - * @param string $authorName - * @return AuthorTag - */ - public function setAuthorName($authorName) + public function setAuthorName(string $authorName) : self { $this->authorName = $authorName; return $this; } - /** - * @return string - */ - public function getAuthorName() + public function getAuthorName() : ?string { return $this->authorName; } - /** - * @return string - */ - public function generate() + public function generate() : string { - $output = '@author' + return '@author' . (! empty($this->authorName) ? ' ' . $this->authorName : '') . (! empty($this->authorEmail) ? ' <' . $this->authorEmail . '>' : ''); - - return $output; } } diff --git a/src/Generator/DocBlock/Tag/GenericTag.php b/src/Generator/DocBlock/Tag/GenericTag.php index 3bd9f980..3ba528c7 100644 --- a/src/Generator/DocBlock/Tag/GenericTag.php +++ b/src/Generator/DocBlock/Tag/GenericTag.php @@ -41,50 +41,31 @@ public function __construct($name = null, $content = null) } } - /** - * @param string $name - * @return GenericTag - */ - public function setName($name) + public function setName($name) : self { $this->name = ltrim($name, '@'); return $this; } - /** - * @return string - */ - public function getName() + public function getName() : string { return $this->name; } - /** - * @param string $content - * @return GenericTag - */ - public function setContent($content) + public function setContent(string $content) : self { $this->content = $content; return $this; } - /** - * @return string - */ - public function getContent() + public function getContent() : ?string { return $this->content; } - /** - * @return string - */ - public function generate() + public function generate() : string { - $output = '@' . $this->name + return '@' . $this->name . (! empty($this->content) ? ' ' . $this->content : ''); - - return $output; } } diff --git a/src/Generator/DocBlock/Tag/LicenseTag.php b/src/Generator/DocBlock/Tag/LicenseTag.php index 6e6d2d5a..ca27df1a 100644 --- a/src/Generator/DocBlock/Tag/LicenseTag.php +++ b/src/Generator/DocBlock/Tag/LicenseTag.php @@ -41,70 +41,46 @@ public function __construct($url = null, $licenseName = null) } /** - * @param ReflectionTagInterface $reflectionTag - * @return ReturnTag * @deprecated Deprecated in 2.3. Use TagManager::createTagFromReflection() instead */ - public static function fromReflection(ReflectionTagInterface $reflectionTag) + public static function fromReflection(ReflectionTagInterface $reflectionTag) : self { $tagManager = new TagManager(); $tagManager->initializeDefaultTags(); return $tagManager->createTagFromReflection($reflectionTag); } - /** - * @return string - */ - public function getName() + public function getName() : string { return 'license'; } - /** - * @param string $url - * @return LicenseTag - */ - public function setUrl($url) + public function setUrl(string $url) : self { $this->url = $url; return $this; } - /** - * @return string - */ - public function getUrl() + public function getUrl() : ?string { return $this->url; } - /** - * @param string $name - * @return LicenseTag - */ - public function setLicenseName($name) + public function setLicenseName(string $name) : self { $this->licenseName = $name; return $this; } - /** - * @return string - */ - public function getLicenseName() + public function getLicenseName() : ?string { return $this->licenseName; } - /** - * @return string - */ - public function generate() + public function generate() : string { - $output = '@license' + return '@license' . (! empty($this->url) ? ' ' . $this->url : '') . (! empty($this->licenseName) ? ' ' . $this->licenseName : ''); - - return $output; } } diff --git a/src/Generator/DocBlock/Tag/MethodTag.php b/src/Generator/DocBlock/Tag/MethodTag.php index 05776e92..5b30a649 100644 --- a/src/Generator/DocBlock/Tag/MethodTag.php +++ b/src/Generator/DocBlock/Tag/MethodTag.php @@ -40,46 +40,29 @@ public function __construct($methodName = null, $types = [], $description = null parent::__construct($types, $description); } - /** - * @return string - */ - public function getName() + public function getName() : string { return 'method'; } - /** - * @param bool $isStatic - * @return MethodTag - */ - public function setIsStatic($isStatic) + public function setIsStatic(bool $isStatic) : self { $this->isStatic = $isStatic; return $this; } - /** - * @return bool - */ - public function isStatic() + public function isStatic() : bool { return $this->isStatic; } - /** - * @param string $methodName - * @return MethodTag - */ - public function setMethodName($methodName) + public function setMethodName(string $methodName) : self { $this->methodName = rtrim($methodName, ')('); return $this; } - /** - * @return string - */ - public function getMethodName() + public function getMethodName() : ?string { return $this->methodName; } @@ -87,14 +70,12 @@ public function getMethodName() /** * @return string */ - public function generate() + public function generate() : string { - $output = '@method' + return '@method' . ($this->isStatic ? ' static' : '') . (! empty($this->types) ? ' ' . $this->getTypesAsString() : '') . (! empty($this->methodName) ? ' ' . $this->methodName . '()' : '') . (! empty($this->description) ? ' ' . $this->description : ''); - - return $output; } } diff --git a/src/Generator/DocBlock/Tag/ParamTag.php b/src/Generator/DocBlock/Tag/ParamTag.php index 9c0f3a8a..89ce6c66 100644 --- a/src/Generator/DocBlock/Tag/ParamTag.php +++ b/src/Generator/DocBlock/Tag/ParamTag.php @@ -36,91 +36,69 @@ public function __construct($variableName = null, $types = [], $description = nu } /** - * @param ReflectionTagInterface $reflectionTag - * @return ReturnTag * @deprecated Deprecated in 2.3. Use TagManager::createTagFromReflection() instead */ - public static function fromReflection(ReflectionTagInterface $reflectionTag) + public static function fromReflection(ReflectionTagInterface $reflectionTag) : self { $tagManager = new TagManager(); $tagManager->initializeDefaultTags(); return $tagManager->createTagFromReflection($reflectionTag); } - /** - * @return string - */ - public function getName() + public function getName() : string { return 'param'; } - /** - * @param string $variableName - * @return ParamTag - */ - public function setVariableName($variableName) + public function setVariableName(string $variableName) : self { $this->variableName = ltrim($variableName, '$'); return $this; } - /** - * @return string - */ - public function getVariableName() + public function getVariableName() : ?string { return $this->variableName; } /** - * @param string $datatype - * @return ReturnTag + * @param string|string[] $datatype * @deprecated Deprecated in 2.3. Use setTypes() instead */ - public function setDatatype($datatype) + public function setDatatype($datatype) : self { return $this->setTypes($datatype); } /** - * @return string * @deprecated Deprecated in 2.3. Use getTypes() or getTypesAsString() instead */ - public function getDatatype() + public function getDatatype() : string { return $this->getTypesAsString(); } /** - * @param string $paramName - * @return ParamTag * @deprecated Deprecated in 2.3. Use setVariableName() instead */ - public function setParamName($paramName) + public function setParamName(string $paramName) : self { return $this->setVariableName($paramName); } /** - * @return string * @deprecated Deprecated in 2.3. Use getVariableName() instead */ - public function getParamName() + public function getParamName() : ?string { return $this->getVariableName(); } - /** - * @return string - */ - public function generate() + public function generate() : string { - $output = '@param' + return '@param' . (! empty($this->types) ? ' ' . $this->getTypesAsString() : '') . (! empty($this->variableName) ? ' $' . $this->variableName : '') . (! empty($this->description) ? ' ' . $this->description : ''); - - return $output; } } diff --git a/src/Generator/DocBlock/Tag/PropertyTag.php b/src/Generator/DocBlock/Tag/PropertyTag.php index 6d4eb474..a8f3ccda 100644 --- a/src/Generator/DocBlock/Tag/PropertyTag.php +++ b/src/Generator/DocBlock/Tag/PropertyTag.php @@ -32,36 +32,23 @@ public function __construct($propertyName = null, $types = [], $description = nu parent::__construct($types, $description); } - /** - * @return string - */ - public function getName() + public function getName() : string { return 'property'; } - /** - * @param string $propertyName - * @return self - */ - public function setPropertyName($propertyName) + public function setPropertyName(string $propertyName) : self { $this->propertyName = ltrim($propertyName, '$'); return $this; } - /** - * @return string - */ - public function getPropertyName() + public function getPropertyName() : ?string { return $this->propertyName; } - /** - * @return string - */ - public function generate() + public function generate() : string { $output = '@property' . (! empty($this->types) ? ' ' . $this->getTypesAsString() : '') diff --git a/src/Generator/DocBlock/Tag/ReturnTag.php b/src/Generator/DocBlock/Tag/ReturnTag.php index 2f2b774d..59a4bae5 100644 --- a/src/Generator/DocBlock/Tag/ReturnTag.php +++ b/src/Generator/DocBlock/Tag/ReturnTag.php @@ -15,40 +15,33 @@ class ReturnTag extends AbstractTypeableTag implements TagInterface { /** - * @param ReflectionTagInterface $reflectionTag - * @return ReturnTag * @deprecated Deprecated in 2.3. Use TagManager::createTagFromReflection() instead */ - public static function fromReflection(ReflectionTagInterface $reflectionTag) + public static function fromReflection(ReflectionTagInterface $reflectionTag) : self { $tagManager = new TagManager(); $tagManager->initializeDefaultTags(); return $tagManager->createTagFromReflection($reflectionTag); } - /** - * @return string - */ - public function getName() + public function getName() : string { return 'return'; } /** - * @param string $datatype - * @return ReturnTag + * @param string|string[] $datatype * @deprecated Deprecated in 2.3. Use setTypes() instead */ - public function setDatatype($datatype) + public function setDatatype($datatype) : self { return $this->setTypes($datatype); } /** - * @return string * @deprecated Deprecated in 2.3. Use getTypes() or getTypesAsString() instead */ - public function getDatatype() + public function getDatatype() : ?string { return $this->getTypesAsString(); } @@ -56,12 +49,10 @@ public function getDatatype() /** * @return string */ - public function generate() + public function generate() : string { - $output = '@return ' + return '@return ' . $this->getTypesAsString() . (! empty($this->description) ? ' ' . $this->description : ''); - - return $output; } } diff --git a/src/Generator/DocBlock/Tag/ThrowsTag.php b/src/Generator/DocBlock/Tag/ThrowsTag.php index a117f98e..2fb73797 100644 --- a/src/Generator/DocBlock/Tag/ThrowsTag.php +++ b/src/Generator/DocBlock/Tag/ThrowsTag.php @@ -11,23 +11,15 @@ class ThrowsTag extends AbstractTypeableTag implements TagInterface { - /** - * @return string - */ - public function getName() + public function getName() : string { return 'throws'; } - /** - * @return string - */ - public function generate() + public function generate() : string { - $output = '@throws' + return '@throws' . (! empty($this->types) ? ' ' . $this->getTypesAsString() : '') . (! empty($this->description) ? ' ' . $this->description : ''); - - return $output; } } diff --git a/src/Generator/DocBlock/TagManager.php b/src/Generator/DocBlock/TagManager.php index 32754132..407b96c2 100644 --- a/src/Generator/DocBlock/TagManager.php +++ b/src/Generator/DocBlock/TagManager.php @@ -27,10 +27,7 @@ */ class TagManager extends PrototypeClassFactory { - /** - * @return void - */ - public function initializeDefaultTags() + public function initializeDefaultTags() : void { $this->addPrototype(new Tag\ParamTag()); $this->addPrototype(new Tag\ReturnTag()); @@ -43,11 +40,7 @@ public function initializeDefaultTags() $this->setGenericPrototype(new Tag\GenericTag()); } - /** - * @param ReflectionTagInterface $reflectionTag - * @return TagInterface - */ - public function createTagFromReflection(ReflectionTagInterface $reflectionTag) + public function createTagFromReflection(ReflectionTagInterface $reflectionTag) : TagInterface { $tagName = $reflectionTag->getName(); @@ -57,12 +50,12 @@ public function createTagFromReflection(ReflectionTagInterface $reflectionTag) // transport any properties via accessors and mutators from reflection to codegen object $reflectionClass = new \ReflectionClass($reflectionTag); foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { - if (substr($method->getName(), 0, 3) == 'get') { + if (0 === strpos($method->getName(), 'get')) { $propertyName = substr($method->getName(), 3); if (method_exists($newTag, 'set' . $propertyName)) { $newTag->{'set' . $propertyName}($reflectionTag->{'get' . $propertyName}()); } - } elseif (substr($method->getName(), 0, 2) == 'is') { + } else if (0 === strpos($method->getName(), 'is')) { $propertyName = ucfirst($method->getName()); if (method_exists($newTag, 'set' . $propertyName)) { $newTag->{'set' . $propertyName}($reflectionTag->{$method->getName()}()); diff --git a/src/Generator/DocBlockGenerator.php b/src/Generator/DocBlockGenerator.php index c3b0f909..4a6cf6eb 100644 --- a/src/Generator/DocBlockGenerator.php +++ b/src/Generator/DocBlockGenerator.php @@ -53,11 +53,8 @@ class DocBlockGenerator extends AbstractGenerator /** * Build a DocBlock generator object from a reflection object - * - * @param DocBlockReflection $reflectionDocBlock - * @return DocBlockGenerator */ - public static function fromReflection(DocBlockReflection $reflectionDocBlock) + public static function fromReflection(DocBlockReflection $reflectionDocBlock) : self { $docBlock = new static(); @@ -82,10 +79,8 @@ public static function fromReflection(DocBlockReflection $reflectionDocBlock) * @configkey tags array * * @throws Exception\InvalidArgumentException - * @param array $array - * @return DocBlockGenerator */ - public static function fromArray(array $array) + public static function fromArray(array $array) : self { $docBlock = new static(); @@ -107,21 +102,16 @@ public static function fromArray(array $array) return $docBlock; } - protected static function getTagManager() + protected static function getTagManager() : TagManager { - if (! isset(static::$tagManager)) { + if (null === static::$tagManager) { static::$tagManager = new TagManager(); static::$tagManager->initializeDefaultTags(); } return static::$tagManager; } - /** - * @param string $shortDescription - * @param string $longDescription - * @param array $tags - */ - public function __construct($shortDescription = null, $longDescription = null, array $tags = []) + public function __construct(?string $shortDescription = null, ?string $longDescription = null, array $tags = []) { if ($shortDescription) { $this->setShortDescription($shortDescription); @@ -134,47 +124,29 @@ public function __construct($shortDescription = null, $longDescription = null, a } } - /** - * @param string $shortDescription - * @return DocBlockGenerator - */ - public function setShortDescription($shortDescription) + public function setShortDescription(string $shortDescription) : self { $this->shortDescription = $shortDescription; return $this; } - /** - * @return string - */ - public function getShortDescription() + public function getShortDescription() : ?string { return $this->shortDescription; } - /** - * @param string $longDescription - * @return DocBlockGenerator - */ - public function setLongDescription($longDescription) + public function setLongDescription(string $longDescription) : self { $this->longDescription = $longDescription; return $this; } - /** - * @return string - */ - public function getLongDescription() + public function getLongDescription() : ?string { return $this->longDescription; } - /** - * @param array $tags - * @return DocBlockGenerator - */ - public function setTags(array $tags) + public function setTags(array $tags) : self { foreach ($tags as $tag) { $this->setTag($tag); @@ -186,9 +158,8 @@ public function setTags(array $tags) /** * @param array|TagInterface $tag * @throws Exception\InvalidArgumentException - * @return DocBlockGenerator */ - public function setTag($tag) + public function setTag($tag) : self { if (is_array($tag)) { // use deprecated Tag class for backward compatibility to old array-keys @@ -210,33 +181,23 @@ public function setTag($tag) /** * @return TagInterface[] */ - public function getTags() + public function getTags() : array { return $this->tags; } - /** - * @param bool $value - * @return DocBlockGenerator - */ - public function setWordWrap($value) + public function setWordWrap(bool $value) : self { $this->wordwrap = (bool) $value; return $this; } - /** - * @return bool - */ - public function getWordWrap() + public function getWordWrap() : bool { return $this->wordwrap; } - /** - * @return string - */ - public function generate() + public function generate() : string { if (! $this->isSourceDirty()) { return $this->docCommentize(trim($this->getSourceContent())); @@ -258,11 +219,7 @@ public function generate() return $this->docCommentize(trim($output)); } - /** - * @param string $content - * @return string - */ - protected function docCommentize($content) + protected function docCommentize(string $content) : string { $indent = $this->getIndentation(); $output = $indent . '/**' . self::LINE_FEED; diff --git a/src/Generator/FileGenerator.php b/src/Generator/FileGenerator.php index 4abcafe3..92a14b40 100644 --- a/src/Generator/FileGenerator.php +++ b/src/Generator/FileGenerator.php @@ -88,25 +88,17 @@ public function __construct($options = null) * Use this if you intend on generating code generation objects based on the same file. * This will keep previous changes to the file in tact during the same PHP process * - * @param string $filePath - * @param bool $includeIfNotAlreadyIncluded * @throws ReflectionException\InvalidArgumentException If file does not exists * @throws ReflectionException\RuntimeException If file exists but is not included or required - * @return FileGenerator */ - public static function fromReflectedFileName($filePath, $includeIfNotAlreadyIncluded = true) + public static function fromReflectedFileName(string $filePath, bool $includeIfNotAlreadyIncluded = true) : self { $fileReflector = new FileReflection($filePath, $includeIfNotAlreadyIncluded); - $codeGenerator = static::fromReflection($fileReflector); - return $codeGenerator; + return static::fromReflection($fileReflector); } - /** - * @param FileReflection $fileReflection - * @return FileGenerator - */ - public static function fromReflection(FileReflection $fileReflection) + public static function fromReflection(FileReflection $fileReflection) : self { $file = new static(); @@ -144,11 +136,7 @@ public static function fromReflection(FileReflection $fileReflection) return $file; } - /** - * @param array $values - * @return FileGenerator - */ - public static function fromArray(array $values) + public static function fromArray(array $values) : self { $fileGenerator = new static(); foreach ($values as $name => $value) { @@ -181,9 +169,8 @@ public static function fromArray(array $values) /** * @param DocBlockGenerator|array|string $docBlock * @throws Exception\InvalidArgumentException - * @return FileGenerator */ - public function setDocBlock($docBlock) + public function setDocBlock($docBlock) : self { if (is_string($docBlock)) { $docBlock = ['shortDescription' => $docBlock]; @@ -203,47 +190,30 @@ public function setDocBlock($docBlock) return $this; } - /** - * @return DocBlockGenerator - */ - public function getDocBlock() + public function getDocBlock() : ?DocBlockGenerator { return $this->docBlock; } - /** - * @param array $requiredFiles - * @return FileGenerator - */ - public function setRequiredFiles(array $requiredFiles) + public function setRequiredFiles(array $requiredFiles) : self { $this->requiredFiles = $requiredFiles; return $this; } - /** - * @return array - */ - public function getRequiredFiles() + public function getRequiredFiles() : array { return $this->requiredFiles; } - /** - * @return string - */ - public function getNamespace() + public function getNamespace() : ?string { return $this->namespace; } - /** - * @param string $namespace - * @return FileGenerator - */ - public function setNamespace($namespace) + public function setNamespace(string $namespace) : self { - $this->namespace = (string) $namespace; + $this->namespace = $namespace; return $this; } @@ -251,35 +221,31 @@ public function setNamespace($namespace) * Returns an array with the first element the use statement, second is the as part. * If $withResolvedAs is set to true, there will be a third element that is the * "resolved" as statement, as the second part is not required in use statements - * - * @param bool $withResolvedAs - * @return array */ - public function getUses($withResolvedAs = false) + public function getUses(bool $withResolvedAs = false) : array { + if (! $withResolvedAs) { + return $this->uses; + } + $uses = $this->uses; - if ($withResolvedAs) { - for ($useIndex = 0, $count = count($uses); $useIndex < $count; $useIndex++) { - if ($uses[$useIndex][1] == '') { - if (($lastSeparator = strrpos($uses[$useIndex][0], '\\')) !== false) { - $uses[$useIndex][2] = substr($uses[$useIndex][0], $lastSeparator + 1); - } else { - $uses[$useIndex][2] = $uses[$useIndex][0]; - } + + foreach (array_keys($uses) as $useIndex) { + if ($uses[$useIndex][1] == '') { + if (($lastSeparator = strrpos($uses[$useIndex][0], '\\')) !== false) { + $uses[$useIndex][2] = substr($uses[$useIndex][0], $lastSeparator + 1); } else { - $uses[$useIndex][2] = $uses[$useIndex][1]; + $uses[$useIndex][2] = $uses[$useIndex][0]; } + } else { + $uses[$useIndex][2] = $uses[$useIndex][1]; } } return $uses; } - /** - * @param array $uses - * @return FileGenerator - */ - public function setUses(array $uses) + public function setUses(array $uses) : self { foreach ($uses as $use) { $use = (array) $use; @@ -287,7 +253,7 @@ public function setUses(array $uses) $import = $use['use']; $alias = $use['as']; } elseif (count($use) == 2) { - list($import, $alias) = $use; + [$import, $alias] = $use; } else { $import = current($use); $alias = null; @@ -297,12 +263,7 @@ public function setUses(array $uses) return $this; } - /** - * @param string $use - * @param null|string $as - * @return FileGenerator - */ - public function setUse($use, $as = null) + public function setUse(string $use, ?string $as = null) : self { if (! in_array([$use, $as], $this->uses)) { $this->uses[] = [$use, $as]; @@ -310,11 +271,7 @@ public function setUse($use, $as = null) return $this; } - /** - * @param array $classes - * @return FileGenerator - */ - public function setClasses(array $classes) + public function setClasses(array $classes) : self { foreach ($classes as $class) { $this->setClass($class); @@ -324,10 +281,10 @@ public function setClasses(array $classes) } /** - * @param string $name - * @return ClassGenerator + * @param string|null $name + * @return ClassGenerator|bool */ - public function getClass($name = null) + public function getClass(?string $name = null) { if ($name === null) { reset($this->classes); @@ -341,9 +298,8 @@ public function getClass($name = null) /** * @param array|string|ClassGenerator $class * @throws Exception\InvalidArgumentException - * @return FileGenerator */ - public function setClass($class) + public function setClass($class) : self { if (is_array($class)) { $class = ClassGenerator::fromArray($class); @@ -364,20 +320,13 @@ public function setClass($class) return $this; } - /** - * @param string $filename - * @return FileGenerator - */ - public function setFilename($filename) + public function setFilename(string $filename) : self { - $this->filename = (string) $filename; + $this->filename = $filename; return $this; } - /** - * @return string - */ - public function getFilename() + public function getFilename() : string { return $this->filename; } @@ -385,33 +334,23 @@ public function getFilename() /** * @return ClassGenerator[] */ - public function getClasses() + public function getClasses() : array { return $this->classes; } - /** - * @param string $body - * @return FileGenerator - */ - public function setBody($body) + public function setBody(string $body) : self { - $this->body = (string) $body; + $this->body = $body; return $this; } - /** - * @return string - */ - public function getBody() + public function getBody() : ?string { return $this->body; } - /** - * @return bool - */ - public function isSourceDirty() + public function isSourceDirty() : bool { $docBlock = $this->getDocBlock(); if ($docBlock && $docBlock->isSourceDirty()) { @@ -427,10 +366,7 @@ public function isSourceDirty() return parent::isSourceDirty(); } - /** - * @return string - */ - public function generate() + public function generate() : string { if ($this->isSourceDirty() === false) { return $this->sourceContent; @@ -518,8 +454,7 @@ public function generate() if (! empty($uses)) { $useOutput = ''; - foreach ($uses as $use) { - list($import, $alias) = $use; + foreach ($uses as [$import, $alias]) { if (null === $alias) { $tempOutput = sprintf('%s', $import); } else { @@ -576,10 +511,9 @@ public function generate() } /** - * @return FileGenerator * @throws Exception\RuntimeException */ - public function write() + public function write() : self { if ($this->filename == '' || ! is_writable(dirname($this->filename))) { throw new Exception\RuntimeException('This code generator object is not writable.'); diff --git a/src/Generator/FileGeneratorRegistry.php b/src/Generator/FileGeneratorRegistry.php index d6dfc633..9ff838af 100644 --- a/src/Generator/FileGeneratorRegistry.php +++ b/src/Generator/FileGeneratorRegistry.php @@ -23,11 +23,9 @@ class FileGeneratorRegistry /** * Registry for the Zend\Code package. * - * @param FileGenerator $fileCodeGenerator - * @param string $fileName * @throws RuntimeException */ - public static function registerFileCodeGenerator(FileGenerator $fileCodeGenerator, $fileName = null) + public static function registerFileCodeGenerator(FileGenerator $fileCodeGenerator, ?string $fileName = null) : void { if ($fileName === null) { $fileName = $fileCodeGenerator->getFilename(); diff --git a/src/Generator/InterfaceGenerator.php b/src/Generator/InterfaceGenerator.php index b810c9e6..72c6f37c 100644 --- a/src/Generator/InterfaceGenerator.php +++ b/src/Generator/InterfaceGenerator.php @@ -22,11 +22,8 @@ class InterfaceGenerator extends ClassGenerator /** * Build a Code Generation Php Object from a Class Reflection - * - * @param ClassReflection $classReflection - * @return InterfaceGenerator */ - public static function fromReflection(ClassReflection $classReflection) + public static function fromReflection(ClassReflection $classReflection) : parent { if (! $classReflection->isInterface()) { throw new Exception\InvalidArgumentException(sprintf( @@ -81,10 +78,8 @@ public static function fromReflection(ClassReflection $classReflection) * @configkey methods * * @throws Exception\InvalidArgumentException - * @param array $array - * @return InterfaceGenerator */ - public static function fromArray(array $array) + public static function fromArray(array $array) : parent { if (! isset($array['name'])) { throw new Exception\InvalidArgumentException( @@ -121,7 +116,7 @@ public static function fromArray(array $array) /** * {@inheritDoc} */ - public function addPropertyFromGenerator(PropertyGenerator $property) + public function addPropertyFromGenerator(PropertyGenerator $property) : parent { return $this; } @@ -129,7 +124,7 @@ public function addPropertyFromGenerator(PropertyGenerator $property) /** * {@inheritDoc} */ - public function addMethodFromGenerator(MethodGenerator $method) + public function addMethodFromGenerator(MethodGenerator $method) : parent { $method->setInterface(true); @@ -139,7 +134,7 @@ public function addMethodFromGenerator(MethodGenerator $method) /** * {@inheritDoc} */ - public function setExtendedClass($extendedClass) + public function setExtendedClass(?string $extendedClass) : parent { return $this; } @@ -147,7 +142,7 @@ public function setExtendedClass($extendedClass) /** * {@inheritDoc} */ - public function setAbstract($isAbstract) + public function setAbstract(bool $isAbstract) : parent { return $this; } diff --git a/src/Generator/MethodGenerator.php b/src/Generator/MethodGenerator.php index 4d75e6b6..c3fc89fe 100644 --- a/src/Generator/MethodGenerator.php +++ b/src/Generator/MethodGenerator.php @@ -27,11 +27,6 @@ class MethodGenerator extends AbstractMemberGenerator { - /** - * @var DocBlockGenerator - */ - protected $docBlock; - /** * @var ParameterGenerator[] */ @@ -52,11 +47,7 @@ class MethodGenerator extends AbstractMemberGenerator */ private $returnsReference = false; - /** - * @param MethodReflection $reflectionMethod - * @return MethodGenerator - */ - public static function fromReflection(MethodReflection $reflectionMethod) + public static function fromReflection(MethodReflection $reflectionMethod) : self { $method = new static(); $declaringClass = $reflectionMethod->getDeclaringClass(); @@ -96,12 +87,8 @@ public static function fromReflection(MethodReflection $reflectionMethod) /** * Identify the space indention from the first line and remove this indention * from all lines - * - * @param string $body - * - * @return string */ - protected static function clearBodyIndention($body) + protected static function clearBodyIndention($body) : string { if (empty($body)) { return $body; @@ -112,7 +99,7 @@ protected static function clearBodyIndention($body) $indention = str_replace(trim($lines[1]), '', $lines[1]); foreach ($lines as $key => $line) { - if (substr($line, 0, strlen($indention)) == $indention) { + if (empty($indention) || 0 === strpos($line, $indention)) { $lines[$key] = substr($line, strlen($indention)); } } @@ -139,7 +126,7 @@ protected static function clearBodyIndention($body) * @param array $array * @return MethodGenerator */ - public static function fromArray(array $array) + public static function fromArray(array $array) : self { if (! isset($array['name'])) { throw new Exception\InvalidArgumentException( @@ -196,10 +183,10 @@ public static function fromArray(array $array) * @param DocBlockGenerator|string $docBlock */ public function __construct( - $name = null, + ?string $name = null, array $parameters = [], - $flags = self::FLAG_PUBLIC, - $body = null, + int $flags = self::FLAG_PUBLIC, + string $body = null, $docBlock = null ) { if ($name) { @@ -219,11 +206,7 @@ public function __construct( } } - /** - * @param array $parameters - * @return MethodGenerator - */ - public function setParameters(array $parameters) + public function setParameters(array $parameters) : self { foreach ($parameters as $parameter) { $this->setParameter($parameter); @@ -235,9 +218,8 @@ public function setParameters(array $parameters) /** * @param ParameterGenerator|array|string $parameter * @throws Exception\InvalidArgumentException - * @return MethodGenerator */ - public function setParameter($parameter) + public function setParameter($parameter) : self { if (is_string($parameter)) { $parameter = new ParameterGenerator($parameter); @@ -263,35 +245,23 @@ public function setParameter($parameter) /** * @return ParameterGenerator[] */ - public function getParameters() + public function getParameters() : array { return $this->parameters; } - /** - * @param string $body - * @return MethodGenerator - */ - public function setBody($body) + public function setBody(?string $body) : self { $this->body = $body; return $this; } - /** - * @return string - */ - public function getBody() + public function getBody() : ?string { return $this->body; } - /** - * @param string|null $returnType - * - * @return MethodGenerator - */ - public function setReturnType($returnType = null) + public function setReturnType(?string $returnType = null) : self { $this->returnType = null === $returnType ? null @@ -300,30 +270,19 @@ public function setReturnType($returnType = null) return $this; } - /** - * @return TypeGenerator|null - */ - public function getReturnType() + public function getReturnType() : ?TypeGenerator { return $this->returnType; } - /** - * @param bool $returnsReference - * - * @return MethodGenerator - */ - public function setReturnsReference($returnsReference) + public function setReturnsReference(bool $returnsReference) : self { - $this->returnsReference = (bool) $returnsReference; + $this->returnsReference = $returnsReference; return $this; } - /** - * @return string - */ - public function generate() + public function generate() : string { $output = ''; @@ -350,6 +309,8 @@ public function generate() $parameters = $this->getParameters(); if (! empty($parameters)) { + $parameterOutput = []; + foreach ($parameters as $parameter) { $parameterOutput[] = $parameter->generate(); } @@ -383,17 +344,12 @@ public function generate() return $output; } - public function __toString() + public function __toString() : string { return $this->generate(); } - /** - * @param MethodReflection $methodReflection - * - * @return null|string - */ - private static function extractReturnTypeFromMethodReflection(MethodReflection $methodReflection) + private static function extractReturnTypeFromMethodReflection(MethodReflection $methodReflection) : ?string { $returnType = method_exists($methodReflection, 'getReturnType') ? $methodReflection->getReturnType() @@ -411,13 +367,7 @@ private static function extractReturnTypeFromMethodReflection(MethodReflection $ . self::expandLiteralType($returnType->getName(), $methodReflection); } - /** - * @param string $literalReturnType - * @param ReflectionMethod $methodReflection - * - * @return string - */ - private static function expandLiteralType($literalReturnType, ReflectionMethod $methodReflection) + private static function expandLiteralType($literalReturnType, ReflectionMethod $methodReflection) : ?string { if ('self' === strtolower($literalReturnType)) { return $methodReflection->getDeclaringClass()->getName(); diff --git a/src/Generator/ParameterGenerator.php b/src/Generator/ParameterGenerator.php index 72b849ec..1750cd96 100644 --- a/src/Generator/ParameterGenerator.php +++ b/src/Generator/ParameterGenerator.php @@ -49,11 +49,7 @@ class ParameterGenerator extends AbstractGenerator */ private $variadic = false; - /** - * @param ParameterReflection $reflectionParameter - * @return ParameterGenerator - */ - public static function fromReflection(ParameterReflection $reflectionParameter) + public static function fromReflection(ParameterReflection $reflectionParameter) : self { $param = new ParameterGenerator(); @@ -95,10 +91,8 @@ public static function fromReflection(ParameterReflection $reflectionParameter) * @configkey sourcecontent string * * @throws Exception\InvalidArgumentException - * @param array $array - * @return ParameterGenerator */ - public static function fromArray(array $array) + public static function fromArray(array $array) : self { if (! isset($array['name'])) { throw new Exception\InvalidArgumentException( @@ -145,11 +139,11 @@ public static function fromArray(array $array) * @param bool $passByReference */ public function __construct( - $name = null, - $type = null, + ?string $name = null, + ?string $type = null, $defaultValue = null, - $position = null, - $passByReference = false + ?int $position = null, + bool $passByReference = false ) { if (null !== $name) { $this->setName($name); @@ -168,41 +162,27 @@ public function __construct( } } - /** - * @param string $type - * @return ParameterGenerator - */ - public function setType($type) + public function setType(string $type) : self { $this->type = TypeGenerator::fromTypeString($type); return $this; } - /** - * @return string - */ - public function getType() + public function getType() : ?string { return $this->type ? (string) $this->type : null; } - /** - * @param string $name - * @return ParameterGenerator - */ - public function setName($name) + public function setName(string $name) : self { $this->name = (string) $name; return $this; } - /** - * @return string - */ - public function getName() + public function getName() : ?string { return $this->name; } @@ -215,7 +195,7 @@ public function getName() * @param null|bool|string|int|float|array|ValueGenerator $defaultValue * @return ParameterGenerator */ - public function setDefaultValue($defaultValue) + public function setDefaultValue($defaultValue) : self { if (! $defaultValue instanceof ValueGenerator) { $defaultValue = new ValueGenerator($defaultValue); @@ -225,74 +205,46 @@ public function setDefaultValue($defaultValue) return $this; } - /** - * @return string - */ - public function getDefaultValue() + public function getDefaultValue() : ?ValueGenerator { return $this->defaultValue; } - /** - * @param int $position - * @return ParameterGenerator - */ - public function setPosition($position) + public function setPosition(int $position) : self { - $this->position = (int) $position; + $this->position = $position; return $this; } - /** - * @return int - */ - public function getPosition() + public function getPosition() : ?int { return $this->position; } - /** - * @return bool - */ - public function getPassedByReference() + public function getPassedByReference() : bool { return $this->passedByReference; } - /** - * @param bool $passedByReference - * @return ParameterGenerator - */ - public function setPassedByReference($passedByReference) + public function setPassedByReference(bool $passedByReference) : self { - $this->passedByReference = (bool) $passedByReference; + $this->passedByReference = $passedByReference; return $this; } - /** - * @param bool $variadic - * - * @return ParameterGenerator - */ - public function setVariadic($variadic) + public function setVariadic(bool $variadic) : self { - $this->variadic = (bool) $variadic; + $this->variadic = $variadic; return $this; } - /** - * @return bool - */ - public function getVariadic() + public function getVariadic() : bool { return $this->variadic; } - /** - * @return string - */ - public function generate() + public function generate() : string { $output = $this->generateTypeHint(); @@ -321,17 +273,8 @@ public function generate() return $output; } - /** - * @param ParameterReflection $reflectionParameter - * - * @return null|string - */ - private static function extractFQCNTypeFromReflectionType(ParameterReflection $reflectionParameter) + private static function extractFQCNTypeFromReflectionType(ParameterReflection $reflectionParameter) : ?string { - if (! method_exists($reflectionParameter, 'getType')) { - return self::prePhp7ExtractFQCNTypeFromReflectionType($reflectionParameter); - } - $type = method_exists($reflectionParameter, 'getType') ? $reflectionParameter->getType() : null; @@ -348,38 +291,16 @@ private static function extractFQCNTypeFromReflectionType(ParameterReflection $r . self::expandLiteralParameterType($type->getName(), $reflectionParameter); } - /** - * For ancient PHP versions (yes, you should upgrade to 7.0): - * - * @param ParameterReflection $reflectionParameter - * - * @return string|null - */ - private static function prePhp7ExtractFQCNTypeFromReflectionType(ParameterReflection $reflectionParameter) - { - if ($reflectionParameter->isCallable()) { - return 'callable'; - } - - if ($reflectionParameter->isArray()) { - return 'array'; - } - - if ($class = $reflectionParameter->getClass()) { - return $class->getName(); - } - - return null; - } - /** * @param string $literalParameterType * @param ReflectionParameter $reflectionParameter * * @return string */ - private static function expandLiteralParameterType($literalParameterType, ReflectionParameter $reflectionParameter) - { + private static function expandLiteralParameterType( + string $literalParameterType, + ReflectionParameter $reflectionParameter + ) : string { if ('self' === strtolower($literalParameterType)) { return $reflectionParameter->getDeclaringClass()->getName(); } @@ -391,10 +312,7 @@ private static function expandLiteralParameterType($literalParameterType, Reflec return $literalParameterType; } - /** - * @return string - */ - private function generateTypeHint() + private function generateTypeHint() : string { if (null === $this->type) { return ''; diff --git a/src/Generator/PropertyGenerator.php b/src/Generator/PropertyGenerator.php index 2a28c24a..8be59ac8 100644 --- a/src/Generator/PropertyGenerator.php +++ b/src/Generator/PropertyGenerator.php @@ -33,7 +33,7 @@ class PropertyGenerator extends AbstractMemberGenerator * @param PropertyReflection $reflectionProperty * @return PropertyGenerator */ - public static function fromReflection(PropertyReflection $reflectionProperty) + public static function fromReflection(PropertyReflection $reflectionProperty) : self { $property = new static(); @@ -80,7 +80,7 @@ public static function fromReflection(PropertyReflection $reflectionProperty) * @param array $array * @return PropertyGenerator */ - public static function fromArray(array $array) + public static function fromArray(array $array) : self { if (! isset($array['name'])) { throw new Exception\InvalidArgumentException( @@ -141,11 +141,7 @@ public function __construct($name = null, $defaultValue = null, $flags = self::F } } - /** - * @param bool $const - * @return PropertyGenerator - */ - public function setConst($const) + public function setConst(bool $const) : self { if ($const) { $this->removeFlag(self::FLAG_PUBLIC | self::FLAG_PRIVATE | self::FLAG_PROTECTED); @@ -157,10 +153,7 @@ public function setConst($const) return $this; } - /** - * @return bool - */ - public function isConst() + public function isConst() : bool { return (bool) ($this->flags & self::FLAG_CONSTANT); } @@ -169,14 +162,12 @@ public function isConst() * @param PropertyValueGenerator|mixed $defaultValue * @param string $defaultValueType * @param string $defaultValueOutputMode - * - * @return PropertyGenerator */ public function setDefaultValue( $defaultValue, - $defaultValueType = PropertyValueGenerator::TYPE_AUTO, - $defaultValueOutputMode = PropertyValueGenerator::OUTPUT_MULTIPLE_LINE - ) { + string $defaultValueType = PropertyValueGenerator::TYPE_AUTO, + string $defaultValueOutputMode = PropertyValueGenerator::OUTPUT_MULTIPLE_LINE + ) : self { if (! $defaultValue instanceof PropertyValueGenerator) { $defaultValue = new PropertyValueGenerator($defaultValue, $defaultValueType, $defaultValueOutputMode); } @@ -186,19 +177,15 @@ public function setDefaultValue( return $this; } - /** - * @return PropertyValueGenerator - */ - public function getDefaultValue() + public function getDefaultValue() : ?PropertyValueGenerator { return $this->defaultValue; } /** * @throws Exception\RuntimeException - * @return string */ - public function generate() + public function generate() : string { $name = $this->getName(); $defaultValue = $this->getDefaultValue(); diff --git a/src/Generator/PropertyValueGenerator.php b/src/Generator/PropertyValueGenerator.php index 19bd475b..b8fc599f 100644 --- a/src/Generator/PropertyValueGenerator.php +++ b/src/Generator/PropertyValueGenerator.php @@ -16,7 +16,7 @@ class PropertyValueGenerator extends ValueGenerator /** * @return string */ - public function generate() + public function generate() : string { return parent::generate() . ';'; } diff --git a/src/Generator/TraitGenerator.php b/src/Generator/TraitGenerator.php index b9174d18..c1eea09f 100644 --- a/src/Generator/TraitGenerator.php +++ b/src/Generator/TraitGenerator.php @@ -24,7 +24,7 @@ class TraitGenerator extends ClassGenerator * @param ClassReflection $classReflection * @return TraitGenerator */ - public static function fromReflection(ClassReflection $classReflection) + public static function fromReflection(ClassReflection $classReflection) : parent { // class generator $cg = new static($classReflection->getName()); @@ -77,7 +77,7 @@ public static function fromReflection(ClassReflection $classReflection) * @param array $array * @return TraitGenerator */ - public static function fromArray(array $array) + public static function fromArray(array $array) : parent { if (! isset($array['name'])) { throw new Exception\InvalidArgumentException( @@ -111,65 +111,37 @@ public static function fromArray(array $array) return $cg; } - /** - * @param array|string $flags - * @return self - */ - public function setFlags($flags) + public function setFlags($flags) : parent { return $this; } - /** - * @param string $flag - * @return self - */ - public function addFlag($flag) + public function addFlag($flag) : parent { return $this; } - /** - * @param string $flag - * @return self - */ - public function removeFlag($flag) + public function removeFlag($flag) : parent { return $this; } - /** - * @param bool $isFinal - * @return self - */ - public function setFinal($isFinal) + public function setFinal(bool $isFinal) : parent { return $this; } - /** - * @param string $extendedClass - * @return self - */ - public function setExtendedClass($extendedClass) + public function setExtendedClass(?string $extendedClass) : parent { return $this; } - /** - * @param array $implementedInterfaces - * @return self - */ - public function setImplementedInterfaces(array $implementedInterfaces) + public function setImplementedInterfaces(array $implementedInterfaces) : parent { return $this; } - /** - * @param bool $isAbstract - * @return self - */ - public function setAbstract($isAbstract) + public function setAbstract(bool $isAbstract) : parent { return $this; } diff --git a/src/Generator/TraitUsageGenerator.php b/src/Generator/TraitUsageGenerator.php index ddc06661..86c04278 100644 --- a/src/Generator/TraitUsageGenerator.php +++ b/src/Generator/TraitUsageGenerator.php @@ -75,16 +75,12 @@ public function addUse($use, $useAlias = null) /** * @inheritDoc */ - public function getUses() + public function getUses() : array { return array_values($this->uses); } - /** - * @param string $use - * @return bool - */ - public function hasUse($use) + public function hasUse(string $use) : bool { foreach ($this->uses as $key => $value) { $parts = explode(' ', $value); @@ -96,11 +92,7 @@ public function hasUse($use) return false; } - /** - * @param string $use - * @return bool - */ - public function hasUseAlias($use) + public function hasUseAlias(string $use) : bool { foreach ($this->uses as $key => $value) { $parts = explode(' as ', $value); @@ -112,11 +104,7 @@ public function hasUseAlias($use) return false; } - /** - * @param string $use - * @return TraitUsageGenerator - */ - public function removeUse($use) + public function removeUse(string $use) : self { foreach ($this->uses as $key => $value) { $parts = explode(' ', $value); @@ -128,11 +116,7 @@ public function removeUse($use) return $this; } - /** - * @param string $use - * @return TraitUsageGenerator - */ - public function removeUseAlias($use) + public function removeUseAlias(string $use) : self { foreach ($this->uses as $key => $value) { $parts = explode(' as ', $value); @@ -147,7 +131,7 @@ public function removeUseAlias($use) /** * @inheritDoc */ - public function addTrait($trait) + public function addTrait($trait) : self { $traitName = $trait; if (is_array($trait)) { @@ -179,7 +163,7 @@ public function addTrait($trait) /** * @inheritDoc */ - public function addTraits(array $traits) + public function addTraits(array $traits) : self { foreach ($traits as $trait) { $this->addTrait($trait); @@ -191,7 +175,7 @@ public function addTraits(array $traits) /** * @inheritDoc */ - public function hasTrait($traitName) + public function hasTrait($traitName) : bool { return in_array($traitName, $this->traits); } @@ -199,7 +183,7 @@ public function hasTrait($traitName) /** * @inheritDoc */ - public function getTraits() + public function getTraits() : array { return $this->traits; } @@ -207,7 +191,7 @@ public function getTraits() /** * @inheritDoc */ - public function removeTrait($traitName) + public function removeTrait($traitName) : self { $key = array_search($traitName, $this->traits); if (false !== $key) { @@ -220,7 +204,7 @@ public function removeTrait($traitName) /** * @inheritDoc */ - public function addTraitAlias($method, $alias, $visibility = null) + public function addTraitAlias($method, $alias, $visibility = null) : self { $traitAndMethod = $method; if (is_array($method)) { @@ -258,7 +242,8 @@ public function addTraitAlias($method, $alias, $visibility = null) ); } - list($trait, $method) = explode('::', $traitAndMethod); + [$trait] = explode('::', $traitAndMethod); + if (! $this->hasTrait($trait)) { throw new Exception\InvalidArgumentException('Invalid trait: Trait does not exists on this class'); } @@ -274,7 +259,7 @@ public function addTraitAlias($method, $alias, $visibility = null) /** * @inheritDoc */ - public function getTraitAliases() + public function getTraitAliases() : array { return $this->traitAliases; } @@ -282,7 +267,7 @@ public function getTraitAliases() /** * @inheritDoc */ - public function addTraitOverride($method, $traitsToReplace) + public function addTraitOverride($method, $traitsToReplace) : self { if (false === is_array($traitsToReplace)) { $traitsToReplace = [$traitsToReplace]; @@ -308,7 +293,7 @@ public function addTraitOverride($method, $traitsToReplace) ); } - list($trait, $method) = explode('::', $traitAndMethod); + [$trait] = explode('::', $traitAndMethod); if (! $this->hasTrait($trait)) { throw new Exception\InvalidArgumentException('Invalid trait: Trait does not exists on this class'); } @@ -335,7 +320,7 @@ public function addTraitOverride($method, $traitsToReplace) /** * @inheritDoc */ - public function removeTraitOverride($method, $overridesToRemove = null) + public function removeTraitOverride($method, $overridesToRemove = null) : self { if (! array_key_exists($method, $this->traitOverrides)) { return $this; @@ -346,10 +331,11 @@ public function removeTraitOverride($method, $overridesToRemove = null) return $this; } - $overridesToRemove = ! is_array($overridesToRemove) - ? [$overridesToRemove] - : $overridesToRemove; - foreach ($overridesToRemove as $traitToRemove) { + $toRemove = is_array($overridesToRemove) + ? $overridesToRemove + : [$overridesToRemove]; + + foreach ($toRemove as $traitToRemove) { $key = array_search($traitToRemove, $this->traitOverrides[$method]); if (false !== $key) { unset($this->traitOverrides[$method][$key]); @@ -361,7 +347,7 @@ public function removeTraitOverride($method, $overridesToRemove = null) /** * @inheritDoc */ - public function getTraitOverrides() + public function getTraitOverrides() : array { return $this->traitOverrides; } @@ -369,7 +355,7 @@ public function getTraitOverrides() /** * @inheritDoc */ - public function generate() + public function generate() : string { $output = ''; $indent = $this->getIndentation(); @@ -390,8 +376,9 @@ public function generate() $output .= ' {' . self::LINE_FEED; foreach ($aliases as $method => $alias) { + $modifierNames = Reflection::getModifierNames($alias['visibility']); $visibility = null !== $alias['visibility'] - ? current(Reflection::getModifierNames($alias['visibility'])) . ' ' + ? current($modifierNames) . ' ' : ''; // validation check diff --git a/src/Generator/TypeGenerator.php b/src/Generator/TypeGenerator.php index 22e09e62..d35c2cb1 100644 --- a/src/Generator/TypeGenerator.php +++ b/src/Generator/TypeGenerator.php @@ -60,16 +60,12 @@ final class TypeGenerator implements GeneratorInterface . '(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/'; /** - * @param string $type - * - * @return TypeGenerator - * * @throws InvalidArgumentException */ - public static function fromTypeString($type) + public static function fromTypeString(string $type) : self { - list($nullable, $trimmedNullable) = self::trimNullable($type); - list($wasTrimmed, $trimmedType) = self::trimType($trimmedNullable); + [$nullable, $trimmedNullable] = self::trimNullable($type); + [$wasTrimmed, $trimmedType] = self::trimType($trimmedNullable); if (! preg_match(self::$validIdentifierMatcher, $trimmedType)) { throw new InvalidArgumentException(sprintf( @@ -108,7 +104,7 @@ private function __construct() /** * {@inheritDoc} */ - public function generate() + public function generate() : string { $nullable = $this->nullable ? '?' : ''; @@ -122,18 +118,16 @@ public function generate() /** * @return string the cleaned type string */ - public function __toString() + public function __toString() : string { return ltrim($this->generate(), '?\\'); } /** - * @param string $type - * * @return bool[]|string[] ordered tuple, first key represents whether the type is nullable, second is the * trimmed string */ - private static function trimNullable($type) + private static function trimNullable(string $type) : array { if (0 === strpos($type, '?')) { return [true, substr($type, 1)]; @@ -143,12 +137,10 @@ private static function trimNullable($type) } /** - * @param string $type - * * @return bool[]|string[] ordered tuple, first key represents whether the values was trimmed, second is the * trimmed string */ - private static function trimType($type) + private static function trimType(string $type) : array { if (0 === strpos($type, '\\')) { return [true, substr($type, 1)]; @@ -157,12 +149,7 @@ private static function trimType($type) return [false, $type]; } - /** - * @param string $type - * - * @return bool - */ - private static function isInternalPhpType($type) + private static function isInternalPhpType(string $type) : bool { return in_array(strtolower($type), self::$internalPhpTypes, true); } diff --git a/src/Generator/ValueGenerator.php b/src/Generator/ValueGenerator.php index c7b552d9..ccb8d8fc 100644 --- a/src/Generator/ValueGenerator.php +++ b/src/Generator/ValueGenerator.php @@ -96,8 +96,8 @@ class ValueGenerator extends AbstractGenerator */ public function __construct( $value = null, - $type = self::TYPE_AUTO, - $outputMode = self::OUTPUT_MULTIPLE_LINE, + string $type = self::TYPE_AUTO, + string $outputMode = self::OUTPUT_MULTIPLE_LINE, $constants = null ) { // strict check is important here if $type = AUTO @@ -123,7 +123,7 @@ public function __construct( /** * Init constant list by defined and magic constants */ - public function initEnvironmentConstants() + public function initEnvironmentConstants() : void { $constants = [ '__DIR__', @@ -142,12 +142,8 @@ public function initEnvironmentConstants() /** * Add constant to list - * - * @param string $constant - * - * @return $this */ - public function addConstant($constant) + public function addConstant(string $constant) : self { $this->constants->append($constant); @@ -156,12 +152,8 @@ public function addConstant($constant) /** * Delete constant from constant list - * - * @param string $constant - * - * @return bool */ - public function deleteConstant($constant) + public function deleteConstant(string $constant) : bool { if (($index = array_search($constant, $this->constants->getArrayCopy())) !== false) { $this->constants->offsetUnset($index); @@ -180,12 +172,9 @@ public function getConstants() return $this->constants; } - /** - * @return bool - */ - public function isValidConstantType() + public function isValidConstantType() : bool { - if ($this->type == self::TYPE_AUTO) { + if ($this->type === self::TYPE_AUTO) { $type = $this->getAutoDeterminedType($this->value); } else { $type = $this->type; @@ -212,9 +201,8 @@ public function isValidConstantType() /** * @param mixed $value - * @return ValueGenerator */ - public function setValue($value) + public function setValue($value) : self { $this->value = $value; return $this; @@ -228,47 +216,29 @@ public function getValue() return $this->value; } - /** - * @param string $type - * @return ValueGenerator - */ - public function setType($type) + public function setType(string $type) : self { - $this->type = (string) $type; + $this->type = $type; return $this; } - /** - * @return string - */ - public function getType() + public function getType() : string { return $this->type; } - /** - * @param int $arrayDepth - * @return ValueGenerator - */ - public function setArrayDepth($arrayDepth) + public function setArrayDepth(int $arrayDepth) : self { - $this->arrayDepth = (int) $arrayDepth; + $this->arrayDepth = $arrayDepth; return $this; } - /** - * @return int - */ - public function getArrayDepth() + public function getArrayDepth() : int { return $this->arrayDepth; } - /** - * @param string $type - * @return string - */ - protected function getValidatedType($type) + protected function getValidatedType(string $type) : string { $types = [ self::TYPE_AUTO, @@ -296,11 +266,7 @@ protected function getValidatedType($type) return self::TYPE_AUTO; } - /** - * @param mixed $value - * @return string - */ - public function getAutoDeterminedType($value) + public function getAutoDeterminedType($value) : string { switch (gettype($value)) { case 'boolean': @@ -330,9 +296,8 @@ public function getAutoDeterminedType($value) /** * @throws Exception\RuntimeException - * @return string */ - public function generate() + public function generate() : string { $type = $this->type; @@ -451,7 +416,7 @@ public function generate() * @param bool $quote Whether add surrounding quotes or not. * @return string PHP-ready code. */ - public static function escape($input, $quote = true) + public static function escape(string $input, bool $quote = true) : string { $output = addcslashes($input, "\\'"); @@ -467,21 +432,18 @@ public static function escape($input, $quote = true) * @param string $outputMode * @return ValueGenerator */ - public function setOutputMode($outputMode) + public function setOutputMode(string $outputMode) : self { - $this->outputMode = (string) $outputMode; + $this->outputMode = $outputMode; return $this; } - /** - * @return string - */ - public function getOutputMode() + public function getOutputMode() : string { return $this->outputMode; } - public function __toString() + public function __toString() : string { return $this->generate(); } diff --git a/src/Generic/Prototype/PrototypeClassFactory.php b/src/Generic/Prototype/PrototypeClassFactory.php index bd96d8b4..feed0e38 100644 --- a/src/Generic/Prototype/PrototypeClassFactory.php +++ b/src/Generic/Prototype/PrototypeClassFactory.php @@ -52,10 +52,9 @@ public function __construct($prototypes = [], PrototypeGenericInterface $generic } /** - * @param PrototypeInterface $prototype * @throws Exception\InvalidArgumentException */ - public function addPrototype(PrototypeInterface $prototype) + public function addPrototype(PrototypeInterface $prototype) : void { $prototypeName = $this->normalizeName($prototype->getName()); @@ -67,57 +66,46 @@ public function addPrototype(PrototypeInterface $prototype) } /** - * @param PrototypeGenericInterface $prototype * @throws Exception\InvalidArgumentException */ - public function setGenericPrototype(PrototypeGenericInterface $prototype) + public function setGenericPrototype(PrototypeGenericInterface $prototype) : void { - if (isset($this->genericPrototype)) { + if ($this->genericPrototype) { throw new Exception\InvalidArgumentException('A default prototype is already set'); } $this->genericPrototype = $prototype; } - /** - * @param string $name - * @return string - */ - protected function normalizeName($name) + protected function normalizeName(string $name) : string { return str_replace(['-', '_'], '', $name); } - /** - * @param string $name - * @return bool - */ - public function hasPrototype($name) + public function hasPrototype(string $name) : bool { $name = $this->normalizeName($name); return isset($this->prototypes[$name]); } /** - * @param string $prototypeName - * @return PrototypeInterface * @throws Exception\RuntimeException */ - public function getClonedPrototype($prototypeName) + public function getClonedPrototype(string $prototypeName) : PrototypeInterface { $prototypeName = $this->normalizeName($prototypeName); - if (! $this->hasPrototype($prototypeName) && ! isset($this->genericPrototype)) { + if (! $this->genericPrototype && ! $this->hasPrototype($prototypeName)) { throw new Exception\RuntimeException('This tag name is not supported by this tag manager'); } if (! $this->hasPrototype($prototypeName)) { $newPrototype = clone $this->genericPrototype; $newPrototype->setName($prototypeName); - } else { - $newPrototype = clone $this->prototypes[$prototypeName]; + + return $newPrototype; } - return $newPrototype; + return clone $this->prototypes[$prototypeName]; } } diff --git a/src/NameInformation.php b/src/NameInformation.php index 8c096642..fb1e33d6 100644 --- a/src/NameInformation.php +++ b/src/NameInformation.php @@ -48,37 +48,23 @@ public function __construct($namespace = null, array $uses = []) } } - /** - * @param string $namespace - * @return NameInformation - */ - public function setNamespace($namespace) + public function setNamespace(string $namespace) : self { - $this->namespace = (string) $namespace; + $this->namespace = $namespace; return $this; } - /** - * @return string - */ - public function getNamespace() + public function getNamespace() : ?string { return $this->namespace; } - /** - * @return bool - */ - public function hasNamespace() + public function hasNamespace() : bool { return $this->namespace !== null; } - /** - * @param array $uses - * @return NameInformation - */ - public function setUses(array $uses) + public function setUses(array $uses) : self { $this->uses = []; $this->addUses($uses); @@ -86,11 +72,7 @@ public function setUses(array $uses) return $this; } - /** - * @param array $uses - * @return NameInformation - */ - public function addUses(array $uses) + public function addUses(array $uses) : self { foreach ($uses as $use => $as) { if (is_int($use)) { @@ -105,9 +87,9 @@ public function addUses(array $uses) /** * @param array|string $use - * @param string $as + * @param string|null $as */ - public function addUse($use, $as = null) + public function addUse($use, ?string $as = null) : void { if (is_array($use) && array_key_exists('use', $use) && array_key_exists('as', $use)) { $uses = $use; @@ -120,7 +102,7 @@ public function addUse($use, $as = null) $as = trim($use, '\\'); $nsSeparatorPosition = strrpos($as, '\\'); if ($nsSeparatorPosition !== false && $nsSeparatorPosition !== 0 && $nsSeparatorPosition != strlen($as)) { - $as = substr($as, $nsSeparatorPosition + 1); + $as = (string) substr($as, $nsSeparatorPosition + 1); } } @@ -130,16 +112,12 @@ public function addUse($use, $as = null) /** * @return array */ - public function getUses() + public function getUses() : array { return $this->uses; } - /** - * @param string $name - * @return string - */ - public function resolveName($name) + public function resolveName(string $name) : string { if ($this->namespace && ! $this->uses && strlen($name) > 0 && $name{0} != '\\') { return $this->namespace . '\\' . $name; diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index 10b0c19f..7adcaa7c 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -35,31 +35,24 @@ class ClassReflection extends ReflectionClass implements ReflectionInterface */ protected $docBlock; - /** - * Return the reflection file of the declaring file. - * - * @return FileReflection - */ - public function getDeclaringFile() + public function getDeclaringFile() : FileReflection { - $instance = new FileReflection($this->getFileName()); - - return $instance; + return new FileReflection($this->getFileName()); } /** * Return the classes DocBlock reflection object * - * @return DocBlockReflection + * @return DocBlockReflection|bool * @throws Exception\ExceptionInterface for missing DocBock or invalid reflection class */ public function getDocBlock() { - if (isset($this->docBlock)) { + if (null !== $this->docBlock) { return $this->docBlock; } - if ('' == $this->getDocComment()) { + if ('' === $this->getDocComment()) { return false; } @@ -70,7 +63,7 @@ public function getDocBlock() /** * @param AnnotationManager $annotationManager - * @return AnnotationCollection + * @return AnnotationCollection|bool */ public function getAnnotations(AnnotationManager $annotationManager) { @@ -96,13 +89,7 @@ public function getAnnotations(AnnotationManager $annotationManager) return $this->annotations; } - /** - * Return the start line of the class - * - * @param bool $includeDocComment - * @return int - */ - public function getStartLine($includeDocComment = false) + public function getStartLine(bool $includeDocComment = false) : int { if ($includeDocComment && $this->getDocComment() != '') { return $this->getDocBlock()->getStartLine(); @@ -111,13 +98,7 @@ public function getStartLine($includeDocComment = false) return parent::getStartLine(); } - /** - * Return the contents of the class - * - * @param bool $includeDocBlock - * @return string - */ - public function getContents($includeDocBlock = true) + public function getContents($includeDocBlock = true) : string { $fileName = $this->getFileName(); @@ -141,31 +122,22 @@ public function getContents($includeDocBlock = true) * * @return ClassReflection[] */ - public function getInterfaces() + public function getInterfaces() : array { - $phpReflections = parent::getInterfaces(); - $zendReflections = []; - while ($phpReflections && ($phpReflection = array_shift($phpReflections))) { - $instance = new ClassReflection($phpReflection->getName()); - $zendReflections[] = $instance; - unset($phpReflection); - } - unset($phpReflections); - - return $zendReflections; + return array_values(array_map( + function (\ReflectionClass $phpReflection) : ClassReflection { + return new ClassReflection($phpReflection->getName()); + }, + parent::getInterfaces() + )); } /** * Return method reflection by name - * - * @param string $name - * @return MethodReflection */ - public function getMethod($name) + public function getMethod($name) : MethodReflection { - $method = new MethodReflection($this->getName(), parent::getMethod($name)->getName()); - - return $method; + return new MethodReflection($this->getName(), parent::getMethod($name)->getName()); } /** @@ -174,28 +146,25 @@ public function getMethod($name) * @param int $filter * @return MethodReflection[] */ - public function getMethods($filter = -1) + public function getMethods($filter = -1) : array { - $methods = []; - foreach (parent::getMethods($filter) as $method) { - $instance = new MethodReflection($this->getName(), $method->getName()); - $methods[] = $instance; - } - - return $methods; + return array_values(array_map( + function (\ReflectionMethod $method) : MethodReflection { + return new MethodReflection($this->getName(), $method->getName()); + }, + parent::getMethods($filter) + )); } /** * Returns an array of reflection classes of traits used by this class. - * - * @return void|array */ - public function getTraits() + public function getTraits() : ?array { $vals = []; $traits = parent::getTraits(); if ($traits === null) { - return; + return null; } foreach ($traits as $trait) { @@ -213,29 +182,22 @@ public function getTraits() public function getParentClass() { $phpReflection = parent::getParentClass(); - if ($phpReflection) { - $zendReflection = new ClassReflection($phpReflection->getName()); - unset($phpReflection); - return $zendReflection; + if (! $phpReflection) { + return false; } - return false; + return new ClassReflection($phpReflection->getName()); } /** * Return reflection property of this class by name * * @param string $name - * @return PropertyReflection */ - public function getProperty($name) + public function getProperty($name) : PropertyReflection { - $phpReflection = parent::getProperty($name); - $zendReflection = new PropertyReflection($this->getName(), $phpReflection->getName()); - unset($phpReflection); - - return $zendReflection; + return new PropertyReflection($this->getName(), parent::getProperty($name)->getName()); } /** @@ -244,7 +206,7 @@ public function getProperty($name) * @param int $filter * @return PropertyReflection[] */ - public function getProperties($filter = -1) + public function getProperties($filter = -1) : array { $phpReflections = parent::getProperties($filter); $zendReflections = []; @@ -261,15 +223,7 @@ public function getProperties($filter = -1) /** * @return string */ - public function toString() - { - return parent::__toString(); - } - - /** - * @return string - */ - public function __toString() + public function toString() : string { return parent::__toString(); } @@ -279,12 +233,8 @@ public function __toString() * * By having this as a separate method it allows the method to be overridden * if a different FileScanner is needed. - * - * @param string $filename - * - * @return FileScanner */ - protected function createFileScanner($filename) + protected function createFileScanner(string $filename) : FileScanner { return new FileScanner($filename); } diff --git a/src/Reflection/DocBlock/Tag/AuthorTag.php b/src/Reflection/DocBlock/Tag/AuthorTag.php index e565fabf..937e6b6c 100644 --- a/src/Reflection/DocBlock/Tag/AuthorTag.php +++ b/src/Reflection/DocBlock/Tag/AuthorTag.php @@ -24,20 +24,15 @@ class AuthorTag implements TagInterface */ protected $authorEmail; - /** - * @return string - */ - public function getName() + public function getName() : string { return 'author'; } /** - * Initializer - * * @param string $tagDocblockLine */ - public function initialize($tagDocblockLine) + public function initialize($tagDocblockLine) : void { $match = []; @@ -54,23 +49,17 @@ public function initialize($tagDocblockLine) } } - /** - * @return null|string - */ - public function getAuthorName() + public function getAuthorName() : ?string { return $this->authorName; } - /** - * @return null|string - */ - public function getAuthorEmail() + public function getAuthorEmail() : ?string { return $this->authorEmail; } - public function __toString() + public function __toString() : string { return 'DocBlock Tag [ * @' . $this->getName() . ' ]' . "\n"; } diff --git a/src/Reflection/DocBlock/Tag/GenericTag.php b/src/Reflection/DocBlock/Tag/GenericTag.php index 5927660e..d7d85199 100644 --- a/src/Reflection/DocBlock/Tag/GenericTag.php +++ b/src/Reflection/DocBlock/Tag/GenericTag.php @@ -46,19 +46,13 @@ public function __construct($contentSplitCharacter = ' ') /** * @param string $tagDocBlockLine - * @return void */ - public function initialize($tagDocBlockLine) + public function initialize($tagDocBlockLine) : void { $this->parse($tagDocBlockLine); } - /** - * Get annotation tag name - * - * @return string - */ - public function getName() + public function getName() : string { return $this->name; } @@ -71,19 +65,12 @@ public function setName($name) $this->name = $name; } - /** - * @return string - */ - public function getContent() + public function getContent() : string { return $this->content; } - /** - * @param int $position - * @return string - */ - public function returnValue($position) + public function returnValue(int $position) : string { return $this->values[$position]; } @@ -96,7 +83,7 @@ public function returnValue($position) * @todo What should this do? * @return string */ - public function __toString() + public function __toString() : string { return 'DocBlock Tag [ * @' . $this->name . ' ]' . "\n"; } diff --git a/src/Reflection/DocBlock/Tag/LicenseTag.php b/src/Reflection/DocBlock/Tag/LicenseTag.php index 9c70327b..6cc555a2 100644 --- a/src/Reflection/DocBlock/Tag/LicenseTag.php +++ b/src/Reflection/DocBlock/Tag/LicenseTag.php @@ -24,20 +24,15 @@ class LicenseTag implements TagInterface */ protected $licenseName; - /** - * @return string - */ - public function getName() + public function getName() : string { return 'license'; } /** - * Initializer - * * @param string $tagDocblockLine */ - public function initialize($tagDocblockLine) + public function initialize($tagDocblockLine) : void { $match = []; @@ -54,23 +49,17 @@ public function initialize($tagDocblockLine) } } - /** - * @return null|string - */ - public function getUrl() + public function getUrl() : ?string { return $this->url; } - /** - * @return null|string - */ - public function getLicenseName() + public function getLicenseName() : ?string { return $this->licenseName; } - public function __toString() + public function __toString() : string { return 'DocBlock Tag [ * @' . $this->getName() . ' ]' . "\n"; } diff --git a/src/Reflection/DocBlock/Tag/MethodTag.php b/src/Reflection/DocBlock/Tag/MethodTag.php index 5aa40f86..840deddf 100644 --- a/src/Reflection/DocBlock/Tag/MethodTag.php +++ b/src/Reflection/DocBlock/Tag/MethodTag.php @@ -39,10 +39,7 @@ class MethodTag implements TagInterface, PhpDocTypedTagInterface */ protected $isStatic = false; - /** - * @return string - */ - public function getName() + public function getName() : string { return 'method'; } @@ -76,18 +73,11 @@ public function initialize($tagDocblockLine) } /** - * Get return value type - * - * @return void|string * @deprecated 2.0.4 use getTypes instead */ - public function getReturnType() + public function getReturnType() : ?string { - if (empty($this->types)) { - return; - } - - return $this->types[0]; + return $this->types[0] ?? null; } public function getTypes() @@ -95,31 +85,22 @@ public function getTypes() return $this->types; } - /** - * @return string - */ - public function getMethodName() + public function getMethodName() : string { return $this->methodName; } - /** - * @return null|string - */ - public function getDescription() + public function getDescription() : ?string { return $this->description; } - /** - * @return bool - */ - public function isStatic() + public function isStatic() : bool { return $this->isStatic; } - public function __toString() + public function __toString() : string { return 'DocBlock Tag [ * @' . $this->getName() . ' ]' . "\n"; } diff --git a/src/Reflection/DocBlock/Tag/ParamTag.php b/src/Reflection/DocBlock/Tag/ParamTag.php index f39b88d2..58cd3e20 100644 --- a/src/Reflection/DocBlock/Tag/ParamTag.php +++ b/src/Reflection/DocBlock/Tag/ParamTag.php @@ -31,10 +31,7 @@ class ParamTag implements TagInterface, PhpDocTypedTagInterface */ protected $description; - /** - * @return string - */ - public function getName() + public function getName() : string { return 'param'; } @@ -66,10 +63,9 @@ public function initialize($tagDocBlockLine) /** * Get parameter variable type * - * @return string * @deprecated 2.0.4 use getTypes instead */ - public function getType() + public function getType() : string { if (empty($this->types)) { return ''; @@ -83,20 +79,12 @@ public function getTypes() return $this->types; } - /** - * Get parameter name - * - * @return string - */ - public function getVariableName() + public function getVariableName() : ?string { return $this->variableName; } - /** - * @return string - */ - public function getDescription() + public function getDescription() : ?string { return $this->description; } diff --git a/src/Reflection/DocBlock/Tag/PropertyTag.php b/src/Reflection/DocBlock/Tag/PropertyTag.php index d3b5f55b..f8270806 100644 --- a/src/Reflection/DocBlock/Tag/PropertyTag.php +++ b/src/Reflection/DocBlock/Tag/PropertyTag.php @@ -30,10 +30,7 @@ class PropertyTag implements TagInterface, PhpDocTypedTagInterface */ protected $description; - /** - * @return string - */ - public function getName() + public function getName() : string { return 'property'; } @@ -64,16 +61,11 @@ public function initialize($tagDocblockLine) } /** - * @return void|string * @deprecated 2.0.4 use getTypes instead */ - public function getType() + public function getType() : ?string { - if (empty($this->types)) { - return; - } - - return $this->types[0]; + return $this->types[0] ?? null; } public function getTypes() @@ -81,23 +73,17 @@ public function getTypes() return $this->types; } - /** - * @return null|string - */ - public function getPropertyName() + public function getPropertyName() : ?string { return $this->propertyName; } - /** - * @return null|string - */ - public function getDescription() + public function getDescription() : ?string { return $this->description; } - public function __toString() + public function __toString() : string { return 'DocBlock Tag [ * @' . $this->getName() . ' ]' . "\n"; } diff --git a/src/Reflection/DocBlock/Tag/ReturnTag.php b/src/Reflection/DocBlock/Tag/ReturnTag.php index 25113b29..ddf855cd 100644 --- a/src/Reflection/DocBlock/Tag/ReturnTag.php +++ b/src/Reflection/DocBlock/Tag/ReturnTag.php @@ -26,19 +26,15 @@ class ReturnTag implements TagInterface, PhpDocTypedTagInterface */ protected $description; - /** - * @return string - */ - public function getName() + public function getName() : string { return 'return'; } /** * @param string $tagDocBlockLine - * @return void */ - public function initialize($tagDocBlockLine) + public function initialize($tagDocBlockLine) : void { $matches = []; if (! preg_match('#((?:[\w|\\\]+(?:\[\])*\|?)+)(?:\s+(.*))?#s', $tagDocBlockLine, $matches)) { @@ -53,10 +49,9 @@ public function initialize($tagDocBlockLine) } /** - * @return string * @deprecated 2.0.4 use getTypes instead */ - public function getType() + public function getType() : string { if (empty($this->types)) { return ''; @@ -70,10 +65,7 @@ public function getTypes() return $this->types; } - /** - * @return string - */ - public function getDescription() + public function getDescription() : ?string { return $this->description; } diff --git a/src/Reflection/DocBlock/Tag/ThrowsTag.php b/src/Reflection/DocBlock/Tag/ThrowsTag.php index 067b2a60..c462c552 100644 --- a/src/Reflection/DocBlock/Tag/ThrowsTag.php +++ b/src/Reflection/DocBlock/Tag/ThrowsTag.php @@ -25,19 +25,15 @@ class ThrowsTag implements TagInterface, PhpDocTypedTagInterface */ protected $description; - /** - * @return string - */ - public function getName() + public function getName() : string { return 'throws'; } /** * @param string $tagDocBlockLine - * @return void */ - public function initialize($tagDocBlockLine) + public function initialize($tagDocBlockLine) : void { $matches = []; preg_match('#([\w|\\\]+)(?:\s+(.*))?#', $tagDocBlockLine, $matches); @@ -52,26 +48,19 @@ public function initialize($tagDocBlockLine) /** * Get return variable type * - * @return string * @deprecated 2.0.4 use getTypes instead */ - public function getType() + public function getType() : string { return implode('|', $this->getTypes()); } - /** - * @return array - */ - public function getTypes() + public function getTypes() : array { return $this->types; } - /** - * @return string - */ - public function getDescription() + public function getDescription() : ?string { return $this->description; } diff --git a/src/Reflection/DocBlock/TagManager.php b/src/Reflection/DocBlock/TagManager.php index f0b3ffeb..38fc127f 100644 --- a/src/Reflection/DocBlock/TagManager.php +++ b/src/Reflection/DocBlock/TagManager.php @@ -14,10 +14,7 @@ class TagManager extends PrototypeClassFactory { - /** - * @return void - */ - public function initializeDefaultTags() + public function initializeDefaultTags() : void { $this->addPrototype(new Tag\ParamTag()); $this->addPrototype(new Tag\ReturnTag()); @@ -30,12 +27,7 @@ public function initializeDefaultTags() $this->setGenericPrototype(new Tag\GenericTag()); } - /** - * @param string $tagName - * @param string $content - * @return TagInterface - */ - public function createTag($tagName, $content = null) + public function createTag(string $tagName, ?string $content = null) : Tag\TagInterface { /* @var TagInterface $newTag */ $newTag = $this->getClonedPrototype($tagName); diff --git a/src/Reflection/DocBlockReflection.php b/src/Reflection/DocBlockReflection.php index 6fad64af..386cedd5 100644 --- a/src/Reflection/DocBlockReflection.php +++ b/src/Reflection/DocBlockReflection.php @@ -83,7 +83,7 @@ class DocBlockReflection implements ReflectionInterface * @todo What should this do? * @return void */ - public static function export() + public static function export() : void { } @@ -91,7 +91,6 @@ public static function export() * @param Reflector|string $commentOrReflector * @param null|DocBlockTagManager $tagManager * @throws Exception\InvalidArgumentException - * @return DocBlockReflection */ public function __construct($commentOrReflector, DocBlockTagManager $tagManager = null) { @@ -131,10 +130,8 @@ public function __construct($commentOrReflector, DocBlockTagManager $tagManager /** * Retrieve contents of DocBlock - * - * @return string */ - public function getContents() + public function getContents() : string { $this->reflect(); @@ -143,10 +140,8 @@ public function getContents() /** * Get start line (position) of DocBlock - * - * @return int */ - public function getStartLine() + public function getStartLine() : int { $this->reflect(); @@ -155,10 +150,8 @@ public function getStartLine() /** * Get last line (position) of DocBlock - * - * @return int */ - public function getEndLine() + public function getEndLine() : int { $this->reflect(); @@ -167,10 +160,8 @@ public function getEndLine() /** * Get DocBlock short description - * - * @return string */ - public function getShortDescription() + public function getShortDescription() : string { $this->reflect(); @@ -179,10 +170,8 @@ public function getShortDescription() /** * Get DocBlock long description - * - * @return string */ - public function getLongDescription() + public function getLongDescription() : string { $this->reflect(); @@ -191,11 +180,8 @@ public function getLongDescription() /** * Does the DocBlock contain the given annotation tag? - * - * @param string $name - * @return bool */ - public function hasTag($name) + public function hasTag(string $name) : bool { $this->reflect(); foreach ($this->tags as $tag) { @@ -228,10 +214,9 @@ public function getTag($name) /** * Get all DocBlock annotation tags * - * @param string $filter * @return DocBlockTagInterface[] */ - public function getTags($filter = null) + public function getTags(?string $filter = null) : array { $this->reflect(); if ($filter === null || ! is_string($filter)) { @@ -248,12 +233,7 @@ public function getTags($filter = null) return $returnTags; } - /** - * Parse the DocBlock - * - * @return void - */ - protected function reflect() + protected function reflect() : void { if ($this->isReflected) { return; @@ -278,10 +258,7 @@ protected function reflect() $this->isReflected = true; } - /** - * @return string - */ - public function toString() + public function toString() : string { $str = 'DocBlock [ /* DocBlock */ ] {' . "\n\n"; $str .= ' - Tags [' . count($this->tags) . '] {' . "\n"; @@ -297,13 +274,9 @@ public function toString() } /** - * Serialize to string - * - * Required by the Reflector interface - * - * @return string + * {@inheritDoc} */ - public function __toString() + public function __toString() : string { return $this->toString(); } diff --git a/src/Reflection/FileReflection.php b/src/Reflection/FileReflection.php index 706e7b41..3be80d3e 100644 --- a/src/Reflection/FileReflection.php +++ b/src/Reflection/FileReflection.php @@ -31,7 +31,7 @@ class FileReflection implements ReflectionInterface protected $filePath; /** - * @var string + * @var string|null */ protected $docComment; @@ -115,50 +115,38 @@ public function __construct($filename, $includeIfNotAlreadyIncluded = false) * @todo What should this do? * @return void */ - public static function export() + public static function export() : void { } - /** - * Return the file name of the reflected file - * - * @return string - */ - public function getFileName() + public function getFileName() : string { return basename($this->filePath); } /** * Get the start line - Always 1, staying consistent with the Reflection API - * - * @return int */ - public function getStartLine() + public function getStartLine() : int { return $this->startLine; } /** * Get the end line / number of lines - * - * @return int */ - public function getEndLine() + public function getEndLine() : int { return $this->endLine; } - /** - * @return string - */ - public function getDocComment() + public function getDocComment() : ?string { return $this->docComment; } /** - * @return DocBlockReflection + * @return DocBlockReflection|bool */ public function getDocBlock() { @@ -166,35 +154,23 @@ public function getDocBlock() return false; } - $instance = new DocBlockReflection($docComment); - - return $instance; + return new DocBlockReflection($docComment); } /** * @return string[] */ - public function getNamespaces() + public function getNamespaces() : array { return $this->namespaces; } - /** - * @return void|string - */ - public function getNamespace() + public function getNamespace() : ?string { - if (count($this->namespaces) == 0) { - return; - } - - return $this->namespaces[0]; + return $this->namespaces[0] ?? null; } - /** - * @return array - */ - public function getUses() + public function getUses() : array { return $this->uses; } @@ -204,14 +180,14 @@ public function getUses() * * @return ClassReflection[] */ - public function getClasses() + public function getClasses() : array { - $classes = []; - foreach ($this->classes as $class) { - $classes[] = new ClassReflection($class); - } - - return $classes; + return array_values(array_map( + function ($class) : ClassReflection { + return new ClassReflection($class); + }, + $this->classes + )); } /** @@ -219,14 +195,14 @@ public function getClasses() * * @return FunctionReflection[] */ - public function getFunctions() + public function getFunctions() : array { - $functions = []; - foreach ($this->functions as $function) { - $functions[] = new FunctionReflection($function); - } - - return $functions; + return array_values(array_map( + function ($function) : FunctionReflection { + return new FunctionReflection($function); + }, + $this->functions + )); } /** @@ -236,7 +212,7 @@ public function getFunctions() * @return ClassReflection * @throws Exception\InvalidArgumentException for invalid class name or invalid reflection class */ - public function getClass($name = null) + public function getClass($name = null) : ClassReflection { if (null === $name) { reset($this->classes); @@ -257,15 +233,13 @@ public function getClass($name = null) /** * Return the full contents of file - * - * @return string */ - public function getContents() + public function getContents() : string { return file_get_contents($this->filePath); } - public function toString() + public function toString() : string { return ''; // @todo } @@ -278,7 +252,7 @@ public function toString() * @todo What should this serialization look like? * @return string */ - public function __toString() + public function __toString() : string { return ''; } @@ -287,10 +261,8 @@ public function __toString() * This method does the work of "reflecting" the file * * Uses Zend\Code\Scanner\FileScanner to gather file information - * - * @return void */ - protected function reflect() + protected function reflect() : void { $scanner = new CachingFileScanner($this->filePath); $this->docComment = $scanner->getDocComment(); @@ -304,23 +276,20 @@ protected function reflect() * Validate / check a file level DocBlock * * @param array $tokens Array of tokenizer tokens - * @return void */ - protected function checkFileDocBlock($tokens) + protected function checkFileDocBlock(array $tokens) : void { foreach ($tokens as $token) { - $type = $token[0]; - $value = $token[1]; - $lineNum = $token[2]; + [$type, $value, $lineNum] = $token; + if (($type == T_OPEN_TAG) || ($type == T_WHITESPACE)) { continue; - } elseif ($type == T_DOC_COMMENT) { + } + + if ($type == T_DOC_COMMENT) { $this->docComment = $value; $this->startLine = $lineNum + substr_count($value, "\n") + 1; - return; - } else { - // Only whitespace is allowed before file DocBlocks return; } } diff --git a/src/Reflection/FunctionReflection.php b/src/Reflection/FunctionReflection.php index 11501273..61c6b63a 100644 --- a/src/Reflection/FunctionReflection.php +++ b/src/Reflection/FunctionReflection.php @@ -41,9 +41,8 @@ class FunctionReflection extends ReflectionFunction implements ReflectionInterfa * Get function DocBlock * * @throws Exception\InvalidArgumentException - * @return DocBlockReflection */ - public function getDocBlock() + public function getDocBlock() : DocBlockReflection { if ('' == ($comment = $this->getDocComment())) { throw new Exception\InvalidArgumentException(sprintf( @@ -52,18 +51,13 @@ public function getDocBlock() )); } - $instance = new DocBlockReflection($comment); - - return $instance; + return new DocBlockReflection($comment); } /** * Get start line (position) of function - * - * @param bool $includeDocComment - * @return int */ - public function getStartLine($includeDocComment = false) + public function getStartLine(bool $includeDocComment = false) : int { if ($includeDocComment) { if ($this->getDocComment() != '') { @@ -76,11 +70,8 @@ public function getStartLine($includeDocComment = false) /** * Get contents of function - * - * @param bool $includeDocBlock - * @return string */ - public function getContents($includeDocBlock = true) + public function getContents(bool $includeDocBlock = true) : string { $fileName = $this->getFileName(); if (false === $fileName) { @@ -114,7 +105,7 @@ public function getContents($includeDocBlock = true) } else { $name = substr($this->getName(), strrpos($this->getName(), '\\') + 1); preg_match( - '#function\s+' . preg_quote($name) . '\s*\([^\)]*\)\s*{([^{}]+({[^}]+})*[^}]+)?}#', + '#function\s+' . preg_quote($name, '#') . '\s*\([^\)]*\)\s*{([^{}]+({[^}]+})*[^}]+)?}#', $functionLine, $matches ); @@ -131,10 +122,9 @@ public function getContents($includeDocBlock = true) /** * Get method prototype * - * @param string $format - * @return array + * @return array|string */ - public function getPrototype($format = FunctionReflection::PROTOTYPE_AS_ARRAY) + public function getPrototype(string $format = FunctionReflection::PROTOTYPE_AS_ARRAY) { $returnType = 'mixed'; $docBlock = $this->getDocBlock(); @@ -187,38 +177,32 @@ public function getPrototype($format = FunctionReflection::PROTOTYPE_AS_ARRAY) * * @return ParameterReflection[] */ - public function getParameters() + public function getParameters() : array { - $phpReflections = parent::getParameters(); - $zendReflections = []; - while ($phpReflections && ($phpReflection = array_shift($phpReflections))) { - $instance = new ParameterReflection($this->getName(), $phpReflection->getName()); - $zendReflections[] = $instance; - unset($phpReflection); - } - unset($phpReflections); - - return $zendReflections; + return array_values(array_map( + function (\ReflectionParameter $phpReflection) : ParameterReflection { + return new ParameterReflection($this->getName(), $phpReflection->getName()); + }, + parent::getParameters() + )); } /** * Get return type tag * * @throws Exception\InvalidArgumentException - * @return DocBlockReflection */ - public function getReturn() + public function getReturn() : DocBlockReflection { $docBlock = $this->getDocBlock(); + if (! $docBlock->hasTag('return')) { throw new Exception\InvalidArgumentException( 'Function does not specify an @return annotation tag; cannot determine return type' ); } - $tag = $docBlock->getTag('return'); - - return new DocBlockReflection('@return ' . $tag->getDescription()); + return new DocBlockReflection('@return ' . $docBlock->getTag('return')->getDescription()); } /** @@ -270,21 +254,8 @@ public function getBody() return $body; } - /** - * @return string - */ - public function toString() + public function toString() : string { return $this->__toString(); } - - /** - * Required due to bug in php - * - * @return string - */ - public function __toString() - { - return parent::__toString(); - } } diff --git a/src/Reflection/MethodReflection.php b/src/Reflection/MethodReflection.php index d7b75a6c..edbc0db7 100644 --- a/src/Reflection/MethodReflection.php +++ b/src/Reflection/MethodReflection.php @@ -28,6 +28,7 @@ use function token_get_all; use function token_name; use function var_export; +use Zend\Code\Scanner\FileScanner; class MethodReflection extends PhpReflectionMethod implements ReflectionInterface { @@ -57,14 +58,12 @@ public function getDocBlock() return false; } - $instance = new DocBlockReflection($this); - - return $instance; + return new DocBlockReflection($this); } /** * @param AnnotationManager $annotationManager - * @return AnnotationScanner + * @return AnnotationScanner|bool */ public function getAnnotations(AnnotationManager $annotationManager) { @@ -92,9 +91,8 @@ public function getAnnotations(AnnotationManager $annotationManager) * Get start line (position) of method * * @param bool $includeDocComment - * @return int */ - public function getStartLine($includeDocComment = false) + public function getStartLine($includeDocComment = false) : int { if ($includeDocComment) { if ($this->getDocComment() != '') { @@ -105,25 +103,16 @@ public function getStartLine($includeDocComment = false) return parent::getStartLine(); } - /** - * Get reflection of declaring class - * - * @return ClassReflection - */ - public function getDeclaringClass() + public function getDeclaringClass() : ClassReflection { - $phpReflection = parent::getDeclaringClass(); - $zendReflection = new ClassReflection($phpReflection->getName()); - unset($phpReflection); - - return $zendReflection; + return new ClassReflection(parent::getDeclaringClass()->getName()); } /** * Get method prototype * * @param string $format - * @return array + * @return array|string */ public function getPrototype($format = MethodReflection::PROTOTYPE_AS_ARRAY) { @@ -181,55 +170,36 @@ public function getPrototype($format = MethodReflection::PROTOTYPE_AS_ARRAY) * * @return ParameterReflection[] */ - public function getParameters() + public function getParameters() : array { - $phpReflections = parent::getParameters(); - $zendReflections = []; - while ($phpReflections && ($phpReflection = array_shift($phpReflections))) { - $instance = new ParameterReflection( - [$this->getDeclaringClass()->getName(), $this->getName()], - $phpReflection->getName() - ); - $zendReflections[] = $instance; - unset($phpReflection); - } - unset($phpReflections); - - return $zendReflections; + return array_values(array_map( + function (\ReflectionParameter $phpReflection) : ParameterReflection { + return new ParameterReflection( + [$this->getDeclaringClass()->getName(), $this->getName()], + $phpReflection->getName() + ); + }, + parent::getParameters() + )); } - /** - * Get method contents - * - * @param bool $includeDocBlock - * @return string - */ - public function getContents($includeDocBlock = true) + public function getContents(bool $includeDocBlock = true) : string { $docComment = $this->getDocComment(); - $content = $includeDocBlock && ! empty($docComment) ? $docComment . "\n" : ''; - $content .= $this->extractMethodContents(); - return $content; + return ($includeDocBlock && ! empty($docComment) ? $docComment . "\n" : '') + . $this->extractMethodContents(); } - /** - * Get method body - * - * @return string - */ - public function getBody() + public function getBody() : string { return $this->extractMethodContents(true); } /** * Tokenize method string and return concatenated body - * - * @param bool $bodyOnly - * @return string */ - protected function extractMethodContents($bodyOnly = false) + protected function extractMethodContents(bool $bodyOnly = false) : string { $fileName = $this->getFileName(); @@ -348,12 +318,8 @@ protected function extractMethodContents($bodyOnly = false) /** * Take current position and find any whitespace - * - * @param array $haystack - * @param int $position - * @return string */ - protected function extractPrefixedWhitespace($haystack, $position) + protected function extractPrefixedWhitespace(array $haystack, int $position) : string { $content = ''; $count = count($haystack); @@ -366,11 +332,11 @@ protected function extractPrefixedWhitespace($haystack, $position) $tokenValue = is_array($haystack[$i]) ? $haystack[$i][1] : $haystack[$i]; //search only for whitespace - if ($tokenType == 'T_WHITESPACE') { - $content .= $tokenValue; - } else { + if ($tokenType !== 'T_WHITESPACE') { break; } + + $content .= $tokenValue; } return $content; @@ -378,17 +344,13 @@ protected function extractPrefixedWhitespace($haystack, $position) /** * Test for ending brace - * - * @param array $haystack - * @param int $position - * @return bool */ - protected function isEndingBrace($haystack, $position) + protected function isEndingBrace(array $haystack, int $position) : bool { $count = count($haystack); //advance one position - $position = $position + 1; + $position += 1; if ($position == $count) { return true; @@ -441,18 +403,15 @@ protected function isEndingBrace($haystack, $position) return false; } } + + return false; } /** * Test to see if current position is valid function or * closure. Returns true if it's a function and NOT a closure - * - * @param array $haystack - * @param int $position - * @param string $functionName - * @return bool */ - protected function isValidFunction($haystack, $position, $functionName = null) + protected function isValidFunction(array $haystack, int $position, ?string $functionName = null) : bool { $isValid = false; $count = count($haystack); @@ -470,7 +429,9 @@ protected function isValidFunction($haystack, $position, $functionName = null) $isValid = true; break; - } elseif ($tokenValue == '(') { + } + + if ($tokenValue == '(') { break; } } @@ -478,10 +439,7 @@ protected function isValidFunction($haystack, $position, $functionName = null) return $isValid; } - /** - * @return string - */ - public function toString() + public function toString() : string { return parent::__toString(); } @@ -489,7 +447,7 @@ public function toString() /** * @return string */ - public function __toString() + public function __toString() : string { return parent::__toString(); } @@ -499,12 +457,8 @@ public function __toString() * * By having this as a separate method it allows the method to be overridden * if a different FileScanner is needed. - * - * @param string $filename - * - * @return CachingFileScanner */ - protected function createFileScanner($filename) + protected function createFileScanner(string $filename) : FileScanner { return new CachingFileScanner($filename); } diff --git a/src/Reflection/ParameterReflection.php b/src/Reflection/ParameterReflection.php index 4498679e..1a837e14 100644 --- a/src/Reflection/ParameterReflection.php +++ b/src/Reflection/ParameterReflection.php @@ -20,36 +20,19 @@ class ParameterReflection extends ReflectionParameter implements ReflectionInter */ protected $isFromMethod = false; - /** - * Get declaring class reflection object - * - * @return ClassReflection - */ - public function getDeclaringClass() + public function getDeclaringClass() : ClassReflection { - $phpReflection = parent::getDeclaringClass(); - $zendReflection = new ClassReflection($phpReflection->getName()); - unset($phpReflection); - - return $zendReflection; + return new ClassReflection(parent::getDeclaringClass()->getName()); } - /** - * Get class reflection object - * - * @return void|ClassReflection - */ - public function getClass() + public function getClass() : ?ClassReflection { $phpReflection = parent::getClass(); if ($phpReflection === null) { - return; + return null; } - $zendReflection = new ClassReflection($phpReflection->getName()); - unset($phpReflection); - - return $zendReflection; + return new ClassReflection($phpReflection->getName()); } /** @@ -70,12 +53,7 @@ public function getDeclaringFunction() return $zendReflection; } - /** - * Get parameter type - * - * @return string|null - */ - public function detectType() + public function detectType() : ?string { if (method_exists($this, 'getType') && ($type = $this->getType()) @@ -113,10 +91,7 @@ public function detectType() return null; } - /** - * @return string - */ - public function toString() + public function toString() : string { return parent::__toString(); } @@ -124,7 +99,7 @@ public function toString() /** * @return string */ - public function __toString() + public function __toString() : string { return parent::__toString(); } diff --git a/src/Reflection/PropertyReflection.php b/src/Reflection/PropertyReflection.php index c277f8e1..272bed40 100644 --- a/src/Reflection/PropertyReflection.php +++ b/src/Reflection/PropertyReflection.php @@ -13,6 +13,7 @@ use Zend\Code\Annotation\AnnotationManager; use Zend\Code\Scanner\AnnotationScanner; use Zend\Code\Scanner\CachingFileScanner; +use Zend\Code\Scanner\FileScanner; /** * @todo implement line numbers @@ -24,32 +25,13 @@ class PropertyReflection extends PhpReflectionProperty implements ReflectionInte */ protected $annotations; - /** - * Get declaring class reflection object - * - * @return ClassReflection - */ - public function getDeclaringClass() + public function getDeclaringClass() : ClassReflection { - $phpReflection = parent::getDeclaringClass(); - $zendReflection = new ClassReflection($phpReflection->getName()); - unset($phpReflection); - - return $zendReflection; + return new ClassReflection(parent::getDeclaringClass()->getName()); } /** - * Get DocBlock comment - * - * @return string|false False if no DocBlock defined - */ - public function getDocComment() - { - return parent::getDocComment(); - } - - /** - * @return false|DocBlockReflection + * @return bool|DocBlockReflection */ public function getDocBlock() { @@ -57,14 +39,12 @@ public function getDocBlock() return false; } - $docBlockReflection = new DocBlockReflection($docComment); - - return $docBlockReflection; + return new DocBlockReflection($docComment); } /** * @param AnnotationManager $annotationManager - * @return AnnotationScanner + * @return AnnotationScanner|bool */ public function getAnnotations(AnnotationManager $annotationManager) { @@ -89,10 +69,7 @@ public function getAnnotations(AnnotationManager $annotationManager) return $this->annotations; } - /** - * @return string - */ - public function toString() + public function toString() : string { return $this->__toString(); } @@ -102,12 +79,8 @@ public function toString() * * By having this as a separate method it allows the method to be overridden * if a different FileScanner is needed. - * - * @param string $filename - * - * @return CachingFileScanner */ - protected function createFileScanner($filename) + protected function createFileScanner(string $filename) : FileScanner { return new CachingFileScanner($filename); } diff --git a/src/Scanner/AggregateDirectoryScanner.php b/src/Scanner/AggregateDirectoryScanner.php index 43ffaf91..8d2175da 100644 --- a/src/Scanner/AggregateDirectoryScanner.php +++ b/src/Scanner/AggregateDirectoryScanner.php @@ -13,11 +13,6 @@ class AggregateDirectoryScanner extends DirectoryScanner { - /** - * @var bool - */ - protected $isScanned = false; - /** * @param bool $returnScannerClass * @todo not implemented @@ -31,7 +26,7 @@ public function getIncludes($returnScannerClass = false) { } - public function getClasses($returnScannerClass = false, $returnDerivedScannerClass = false) + public function getClasses(bool $returnScannerClass = false, bool $returnDerivedScannerClass = false) : array { $classes = []; foreach ($this->directories as $scanner) { @@ -46,67 +41,54 @@ public function getClasses($returnScannerClass = false, $returnDerivedScannerCla return $classes; } - /** - * @param string $class - * @return bool - */ - public function hasClass($class) + public function hasClass(string $class) : bool { foreach ($this->directories as $scanner) { if ($scanner->hasClass($class)) { - break; - } else { - unset($scanner); + return true; } } - return isset($scanner); + return false; } /** - * @param string $class - * @param bool $returnScannerClass - * @param bool $returnDerivedScannerClass - * @return ClassScanner|DerivedClassScanner * @throws Exception\RuntimeException */ - public function getClass($class, $returnScannerClass = true, $returnDerivedScannerClass = false) - { + public function getClass( + string $class, + bool $returnScannerClass = true, + bool $returnDerivedScannerClass = false + ) : ClassScanner { + $classScanner = null; + foreach ($this->directories as $scanner) { if ($scanner->hasClass($class)) { - break; - } else { - unset($scanner); + $classScanner = $scanner->getClass($class); } } - if (! isset($scanner)) { + if (! $classScanner) { throw new Exception\RuntimeException('Class by that name was not found.'); } - $classScanner = $scanner->getClass($class); - return new DerivedClassScanner($classScanner, $this); } - /** - * @param bool $returnScannerClass - */ - public function getFunctions($returnScannerClass = false) + public function getFunctions(bool $returnScannerClass = false) { $this->scan(); if (! $returnScannerClass) { $functions = []; foreach ($this->infos as $info) { - if ($info['type'] == 'function') { + if ($info['type'] === 'function') { $functions[] = $info['name']; } } return $functions; } - $scannerClass = new FunctionScanner(); // @todo } } diff --git a/src/Scanner/AnnotationScanner.php b/src/Scanner/AnnotationScanner.php index 53a348a0..d27435cf 100644 --- a/src/Scanner/AnnotationScanner.php +++ b/src/Scanner/AnnotationScanner.php @@ -56,7 +56,6 @@ class AnnotationScanner extends AnnotationCollection implements ScannerInterface * @param AnnotationManager $annotationManager * @param string $docComment * @param NameInformation $nameInformation - * @return AnnotationScanner */ public function __construct( AnnotationManager $annotationManager, @@ -69,18 +68,12 @@ public function __construct( $this->scan($this->tokenize()); } - /** - * @param NameInformation $nameInformation - */ - public function setNameInformation(NameInformation $nameInformation) + public function setNameInformation(NameInformation $nameInformation) : void { $this->nameInformation = $nameInformation; } - /** - * @param array $tokens - */ - protected function scan(array $tokens) + protected function scan(array $tokens) : void { $annotations = []; $annotationIndex = -1; @@ -141,10 +134,7 @@ protected function scan(array $tokens) } } - /** - * @return array - */ - protected function tokenize() + protected function tokenize() : array { static $CONTEXT_DOCBLOCK = 0x01; static $CONTEXT_ASTERISK = 0x02; @@ -317,7 +307,7 @@ protected function tokenize() goto TOKENIZER_TOP; } - if ($MACRO_HAS_CONTEXT($CONTEXT_DOCBLOCK) && $currentWord === '*/') { + if ($currentWord === '*/' && $MACRO_HAS_CONTEXT($CONTEXT_DOCBLOCK)) { $MACRO_TOKEN_SET_TYPE('ANNOTATION_COMMENTEND'); $MACRO_TOKEN_APPEND_WORD(); $MACRO_TOKEN_ADVANCE(); @@ -329,7 +319,7 @@ protected function tokenize() } if ($currentChar === '*') { - if ($MACRO_HAS_CONTEXT($CONTEXT_DOCBLOCK) && ($MACRO_HAS_CONTEXT($CONTEXT_ASTERISK))) { + if ($MACRO_HAS_CONTEXT($CONTEXT_DOCBLOCK) && $MACRO_HAS_CONTEXT($CONTEXT_ASTERISK)) { $MACRO_TOKEN_SET_TYPE('ANNOTATION_IGNORE'); } else { $MACRO_TOKEN_SET_TYPE('ANNOTATION_ASTERISK'); @@ -353,8 +343,6 @@ protected function tokenize() goto TOKENIZER_TOP; } - TOKENIZER_CONTINUE: - if ($context && $CONTEXT_CONTENT) { $MACRO_TOKEN_APPEND_CHAR(); if ($MACRO_STREAM_ADVANCE_CHAR() === false) { diff --git a/src/Scanner/CachingFileScanner.php b/src/Scanner/CachingFileScanner.php index 2a9a5524..daecb77a 100644 --- a/src/Scanner/CachingFileScanner.php +++ b/src/Scanner/CachingFileScanner.php @@ -32,11 +32,9 @@ class CachingFileScanner extends FileScanner protected $fileScanner; /** - * @param string $file - * @param AnnotationManager $annotationManager * @throws Exception\InvalidArgumentException */ - public function __construct($file, AnnotationManager $annotationManager = null) + public function __construct(string $file, AnnotationManager $annotationManager = null) { if (! file_exists($file)) { throw new Exception\InvalidArgumentException(sprintf( @@ -45,11 +43,13 @@ public function __construct($file, AnnotationManager $annotationManager = null) )); } - $file = realpath($file); + $file = (string) realpath($file); - $cacheId = md5($file) . '/' . (isset($annotationManager) - ? spl_object_hash($annotationManager) - : 'no-annotation'); + $cacheId = md5($file) . '/' . ( + $annotationManager + ? spl_object_hash($annotationManager) + : 'no-annotation' + ); if (isset(static::$cache[$cacheId])) { $this->fileScanner = static::$cache[$cacheId]; @@ -59,59 +59,42 @@ public function __construct($file, AnnotationManager $annotationManager = null) } } - /** - * @return void - */ - public static function clearCache() + public static function clearCache() : void { static::$cache = []; } - /** - * @return AnnotationManager - */ - public function getAnnotationManager() + public function getAnnotationManager() : ?AnnotationManager { return $this->fileScanner->getAnnotationManager(); } - /** - * @return array|null|string - */ - public function getFile() + public function getFile() : string { return $this->fileScanner->getFile(); } - /** - * @return null|string - */ - public function getDocComment() + public function getDocComment() : ?string { return $this->fileScanner->getDocComment(); } - /** - * @return array - */ - public function getNamespaces() + public function getNamespaces() : array { return $this->fileScanner->getNamespaces(); } - /** - * @param null|string $namespace - * @return array|null - */ - public function getUses($namespace = null) + public function getUses(?string $namespace = null) : ?array { return $this->fileScanner->getUses($namespace); } /** * @return array + * + * Note: nullability on the hint is needed, because the inner object does not implement anything yet */ - public function getIncludes() + public function getIncludes() : ?array { return $this->fileScanner->getIncludes(); } @@ -119,7 +102,7 @@ public function getIncludes() /** * @return array */ - public function getClassNames() + public function getClassNames() : array { return $this->fileScanner->getClassNames(); } @@ -127,16 +110,15 @@ public function getClassNames() /** * @return array */ - public function getClasses() + public function getClasses() : array { return $this->fileScanner->getClasses(); } /** * @param int|string $className - * @return ClassScanner */ - public function getClass($className) + public function getClass($className) : ClassScanner { return $this->fileScanner->getClass($className); } @@ -153,7 +135,7 @@ public function getClassNameInformation($className) /** * @return array */ - public function getFunctionNames() + public function getFunctionNames() : array { return $this->fileScanner->getFunctionNames(); } @@ -161,7 +143,7 @@ public function getFunctionNames() /** * @return array */ - public function getFunctions() + public function getFunctions() : array { return $this->fileScanner->getFunctions(); } diff --git a/src/Scanner/ClassScanner.php b/src/Scanner/ClassScanner.php index ba734e30..e46c3402 100644 --- a/src/Scanner/ClassScanner.php +++ b/src/Scanner/ClassScanner.php @@ -132,7 +132,6 @@ class ClassScanner implements ScannerInterface /** * @param array $classTokens * @param NameInformation|null $nameInformation - * @return ClassScanner */ public function __construct(array $classTokens, NameInformation $nameInformation = null) { @@ -144,7 +143,7 @@ public function __construct(array $classTokens, NameInformation $nameInformation * Get annotations * * @param Annotation\AnnotationManager $annotationManager - * @return Annotation\AnnotationCollection + * @return Annotation\AnnotationCollection|bool */ public function getAnnotations(Annotation\AnnotationManager $annotationManager) { @@ -155,12 +154,7 @@ public function getAnnotations(Annotation\AnnotationManager $annotationManager) return new AnnotationScanner($annotationManager, $docComment, $this->nameInformation); } - /** - * Return documentation comment - * - * @return null|string - */ - public function getDocComment() + public function getDocComment() : ?string { $this->scan(); @@ -170,7 +164,7 @@ public function getDocComment() /** * Return documentation block * - * @return false|DocBlockScanner + * @return bool|DocBlockScanner */ public function getDocBlock() { @@ -181,144 +175,79 @@ public function getDocBlock() return new DocBlockScanner($docComment); } - /** - * Return a name of class - * - * @return null|string - */ - public function getName() + public function getName() : ?string { $this->scan(); return $this->name; } - /** - * Return short name of class - * - * @return null|string - */ - public function getShortName() + public function getShortName() : ?string { $this->scan(); return $this->shortName; } - /** - * Return number of first line - * - * @return int|null - */ - public function getLineStart() + public function getLineStart() : ?int { $this->scan(); return $this->lineStart; } - /** - * Return number of last line - * - * @return int|null - */ - public function getLineEnd() + public function getLineEnd() : ?int { $this->scan(); return $this->lineEnd; } - /** - * Verify if class is final - * - * @return bool - */ - public function isFinal() + public function isFinal() : bool { $this->scan(); return $this->isFinal; } - /** - * Verify if class is a trait - * - * @return bool - */ - public function isTrait() + public function isTrait() : bool { $this->scan(); return $this->isTrait; } - /** - * Verify if class is instantiable - * - * @return bool - */ - public function isInstantiable() + public function isInstantiable() : bool { $this->scan(); return ! $this->isAbstract && ! $this->isInterface && ! $this->isTrait; } - /** - * Verify if class is an abstract class - * - * @return bool - */ - public function isAbstract() + public function isAbstract() : bool { $this->scan(); return $this->isAbstract; } - /** - * Verify if class is an interface - * - * @return bool - */ - public function isInterface() + public function isInterface() : bool { $this->scan(); return $this->isInterface; } - /** - * Verify if class has parent - * - * @return bool - */ - public function hasParentClass() + public function hasParentClass() : bool { $this->scan(); return $this->parentClass !== null; } - /** - * Return a name of parent class - * - * @return null|string - */ - public function getParentClass() + public function getParentClass() : ?string { $this->scan(); return $this->parentClass; } - /** - * Return a list of interface names - * - * @return array - */ - public function getInterfaces() + public function getInterfaces() : array { $this->scan(); return $this->interfaces; } - /** - * Return a list of constant names - * - * @return array - */ - public function getConstantNames() + public function getConstantNames() : array { $this->scan(); @@ -340,7 +269,7 @@ public function getConstantNames() * @param bool $namesOnly Set false to return instances of ConstantScanner * @return array|ConstantScanner[] */ - public function getConstants($namesOnly = true) + public function getConstants(bool $namesOnly = true) : array { if (true === $namesOnly) { trigger_error('Use method getConstantNames() instead', E_USER_DEPRECATED); @@ -405,13 +334,7 @@ public function getConstant($constantNameOrInfoIndex) return $p; } - /** - * Verify if class has constant - * - * @param string $name - * @return bool - */ - public function hasConstant($name) + public function hasConstant(string $name) : bool { $this->scan(); @@ -424,12 +347,7 @@ public function hasConstant($name) return false; } - /** - * Return a list of property names - * - * @return array - */ - public function getPropertyNames() + public function getPropertyNames() : array { $this->scan(); @@ -450,7 +368,7 @@ public function getPropertyNames() * * @return PropertyScanner[] */ - public function getProperties() + public function getProperties() : array { $this->scan(); @@ -510,13 +428,7 @@ public function getProperty($propertyNameOrInfoIndex) return $p; } - /** - * Verify if class has property - * - * @param string $name - * @return bool - */ - public function hasProperty($name) + public function hasProperty(string $name) : bool { $this->scan(); @@ -534,7 +446,7 @@ public function hasProperty($name) * * @return ClassScanner[] */ - public function getTraits() + public function getTraits() : array { if (! empty($this->traits)) { return $this->traits; @@ -562,9 +474,9 @@ public function getTraits() /** * Retrieve a list of trait names used by this class. * - * @return array + * @return string[] */ - public function getTraitNames() + public function getTraitNames() : array { $this->scan(); @@ -591,9 +503,9 @@ public function getTraitNames() /** * Retrieve a list of aliased traits used by the class. * - * @return array + * @return string[] */ - public function getTraitAliases() + public function getTraitAliases() : array { $this->scan(); @@ -612,7 +524,7 @@ public function getTraitAliases() } // attempt to get fqcn - list($trait, $method) = explode('::', $alias['original']); + [$trait, $method] = explode('::', $alias['original']); if ($this->nameInformation instanceof NameInformation) { $trait = $this->nameInformation->resolveName($trait); } @@ -625,13 +537,7 @@ public function getTraitAliases() return $return; } - /** - * Retrieve visibility for a given alias. - * - * @param mixed $aliasName - * @return string - */ - protected function getVisibilityForAlias($aliasName) + protected function getVisibilityForAlias(string $aliasName) : string { $this->scan(); @@ -662,10 +568,8 @@ protected function getVisibilityForAlias($aliasName) /** * Return an array of key = trait to keep, value = trait::method to ignore - * - * @return array */ - protected function getBlockedTraitMethods() + protected function getBlockedTraitMethods() : array { $this->scan(); @@ -684,7 +588,7 @@ protected function getBlockedTraitMethods() } // attempt to get fqcn - list($trait, $method) = explode('::', $alias['original']); + [$trait, $method] = explode('::', $alias['original']); if ($this->nameInformation instanceof NameInformation) { $trait = $this->nameInformation->resolveName($alias['alias']); } @@ -699,10 +603,8 @@ protected function getBlockedTraitMethods() /** * Return a list of method names - * - * @return array */ - public function getMethodNames() + public function getMethodNames() : array { $this->scan(); @@ -720,7 +622,7 @@ public function getMethodNames() * * @return MethodScanner[] */ - public function getMethods() + public function getMethods() : array { $this->scan(); @@ -806,8 +708,8 @@ public function getMethods() * Return a single method by given name or index of info * * @param string|int $methodNameOrInfoIndex + * @return MethodScanner|bool * @throws Exception\InvalidArgumentException - * @return MethodScanner */ public function getMethod($methodNameOrInfoIndex) { @@ -833,13 +735,7 @@ public function getMethod($methodNameOrInfoIndex) return $returnMethod; } - /** - * Verify if class has method by given name - * - * @param string $name - * @return bool - */ - public function hasMethod($name) + public function hasMethod(string $name) : bool { $this->scan(); @@ -851,18 +747,15 @@ public static function export() // @todo } - public function __toString() + public function __toString() : string { // @todo } /** - * Scan tokens - * - * @return void * @throws Exception\RuntimeException */ - protected function scan() + protected function scan() : void { if ($this->isScanned) { return; @@ -912,13 +805,13 @@ protected function scan() if (is_string($token)) { $tokenType = null; $tokenContent = $token; - $tokenLine = $tokenLine + substr_count( + $tokenLine += substr_count( $lastTokenArray[1], "\n" ); // adjust token line by last known newline count } else { $lastTokenArray = $token; - list($tokenType, $tokenContent, $tokenLine) = $token; + [$tokenType, $tokenContent, $tokenLine] = $token; } return $tokenIndex; @@ -1118,7 +1011,6 @@ protected function scan() $isValidAlias = array_merge($isOriginalName, $isAlias, $isVisibility, $isAliasType); $useStatementIndex = 0; - $aliasStatementIndex = 0; $useAliasContext = false; $useAsContext = false; diff --git a/src/Scanner/ConstantScanner.php b/src/Scanner/ConstantScanner.php index 1be19b53..63c4304b 100644 --- a/src/Scanner/ConstantScanner.php +++ b/src/Scanner/ConstantScanner.php @@ -88,33 +88,24 @@ public function setClass($class) $this->class = $class; } - /** - * @param ClassScanner $scannerClass - */ - public function setScannerClass(ClassScanner $scannerClass) + public function setScannerClass(ClassScanner $scannerClass) : void { $this->scannerClass = $scannerClass; } - /** - * @return ClassScanner - */ - public function getClassScanner() + public function getClassScanner() : ?ClassScanner { return $this->scannerClass; } - /** - * @return string - */ - public function getName() + public function getName() : string { $this->scan(); return $this->name; } /** - * @return string + * @return mixed */ public function getValue() { @@ -122,10 +113,7 @@ public function getValue() return $this->value; } - /** - * @return string - */ - public function getDocComment() + public function getDocComment() : ?string { $this->scan(); return $this->docComment; @@ -133,7 +121,7 @@ public function getDocComment() /** * @param Annotation\AnnotationManager $annotationManager - * @return AnnotationScanner + * @return AnnotationScanner|bool */ public function getAnnotations(Annotation\AnnotationManager $annotationManager) { @@ -147,7 +135,7 @@ public function getAnnotations(Annotation\AnnotationManager $annotationManager) /** * @return string */ - public function __toString() + public function __toString() : string { $this->scan(); return var_export($this, true); @@ -180,7 +168,7 @@ protected function scan() $token = current($tokens); if (! is_string($token)) { - list($tokenType, $tokenContent, $tokenLine) = $token; + [$tokenType, $tokenContent] = $token; switch ($tokenType) { case T_DOC_COMMENT: @@ -197,10 +185,10 @@ protected function scan() $this->name = $string; } else { if ('self' == strtolower($string)) { - list($tokenNextType, $tokenNextContent, $tokenNextLine) = next($tokens); + [$unused1, $tokenNextContent, $unused2] = next($tokens); if ('::' == $tokenNextContent) { - list($tokenNextType, $tokenNextContent, $tokenNextLine) = next($tokens); + [$unused1, $tokenNextContent, $unused2] = next($tokens); if ($this->getClassScanner()->getConstant($tokenNextContent)) { $this->value = $this->getClassScanner()->getConstant($tokenNextContent)->getValue(); @@ -217,10 +205,10 @@ protected function scan() case T_LNUMBER: $string = is_string($token) ? $token : $tokenContent; - if (substr($string, 0, 1) === '"' || substr($string, 0, 1) === "'") { + $this->value = $string; + + if (in_array(substr($string, 0, 1), ['"', '\''], true)) { $this->value = substr($string, 1, -1); // Remove quotes - } else { - $this->value = $string; } goto SCANNER_CONTINUE; // fall-trough diff --git a/src/Scanner/DerivedClassScanner.php b/src/Scanner/DerivedClassScanner.php index 21a3fc32..44b40eec 100644 --- a/src/Scanner/DerivedClassScanner.php +++ b/src/Scanner/DerivedClassScanner.php @@ -67,83 +67,52 @@ public function __construct(ClassScanner $classScanner, DirectoryScanner $direct } } - /** - * @return null|string - */ - public function getName() + public function getName() : ?string { return $this->classScanner->getName(); } - /** - * @return null|string - */ - public function getShortName() + public function getShortName() : ?string { return $this->classScanner->getShortName(); } - /** - * @return bool - */ - public function isInstantiable() + public function isInstantiable() : bool { return $this->classScanner->isInstantiable(); } - /** - * @return bool - */ - public function isFinal() + public function isFinal() : bool { return $this->classScanner->isFinal(); } - /** - * @return bool - */ - public function isAbstract() + public function isAbstract() : bool { return $this->classScanner->isAbstract(); } - /** - * @return bool - */ - public function isInterface() + public function isInterface() : bool { return $this->classScanner->isInterface(); } - /** - * @return array - */ - public function getParentClasses() + public function getParentClasses() : array { return array_keys($this->parentClassScanners); } - /** - * @return bool - */ - public function hasParentClass() + public function hasParentClass() : bool { return $this->classScanner->getParentClass() !== null; } - /** - * @return null|string - */ - public function getParentClass() + public function getParentClass() : ?string { return $this->classScanner->getParentClass(); } - /** - * @param bool $returnClassScanners - * @return array - */ - public function getInterfaces($returnClassScanners = false) + public function getInterfaces(bool $returnClassScanners = false) : array { if ($returnClassScanners) { return $this->interfaceClassScanners; @@ -157,12 +126,7 @@ public function getInterfaces($returnClassScanners = false) return $interfaces; } - /** - * Return a list of constant names - * - * @return array - */ - public function getConstantNames() + public function getConstantNames() : array { $constants = $this->classScanner->getConstantNames(); foreach ($this->parentClassScanners as $pClassScanner) { @@ -178,7 +142,7 @@ public function getConstantNames() * @param bool $namesOnly Set false to return instances of ConstantScanner * @return array|ConstantScanner[] */ - public function getConstants($namesOnly = true) + public function getConstants(bool $namesOnly = true) : array { if (true === $namesOnly) { trigger_error('Use method getConstantNames() instead', E_USER_DEPRECATED); @@ -225,7 +189,7 @@ public function getConstant($constantNameOrInfoIndex) * @param string $name * @return bool */ - public function hasConstant($name) + public function hasConstant(string $name) : bool { if ($this->classScanner->hasConstant($name)) { return true; @@ -241,10 +205,8 @@ public function hasConstant($name) /** * Return a list of property names - * - * @return array */ - public function getPropertyNames() + public function getPropertyNames() : array { $properties = $this->classScanner->getPropertyNames(); foreach ($this->parentClassScanners as $pClassScanner) { @@ -254,11 +216,7 @@ public function getPropertyNames() return $properties; } - /** - * @param bool $returnScannerProperty - * @return array - */ - public function getProperties($returnScannerProperty = false) + public function getProperties(bool $returnScannerProperty = false) : array { $properties = $this->classScanner->getProperties($returnScannerProperty); foreach ($this->parentClassScanners as $pClassScanner) { @@ -294,13 +252,7 @@ public function getProperty($propertyNameOrInfoIndex) )); } - /** - * Verify if class or parent class has property - * - * @param string $name - * @return bool - */ - public function hasProperty($name) + public function hasProperty(string $name) : bool { if ($this->classScanner->hasProperty($name)) { return true; @@ -314,10 +266,7 @@ public function hasProperty($name) return false; } - /** - * @return array - */ - public function getMethodNames() + public function getMethodNames() : array { $methods = $this->classScanner->getMethodNames(); foreach ($this->parentClassScanners as $pClassScanner) { @@ -330,9 +279,10 @@ public function getMethodNames() /** * @return MethodScanner[] */ - public function getMethods() + public function getMethods() : array { $methods = $this->classScanner->getMethods(); + foreach ($this->parentClassScanners as $pClassScanner) { $methods = array_merge($methods, $pClassScanner->getMethods()); } @@ -341,9 +291,7 @@ public function getMethods() } /** - * @param int|string $methodNameOrInfoIndex - * @return MethodScanner - * @throws Exception\InvalidArgumentException + * {@inheritDoc} */ public function getMethod($methodNameOrInfoIndex) { @@ -364,13 +312,7 @@ public function getMethod($methodNameOrInfoIndex) )); } - /** - * Verify if class or parent class has method by given name - * - * @param string $name - * @return bool - */ - public function hasMethod($name) + public function hasMethod(string $name) : bool { if ($this->classScanner->hasMethod($name)) { return true; diff --git a/src/Scanner/DirectoryScanner.php b/src/Scanner/DirectoryScanner.php index 552573a5..d6d80061 100644 --- a/src/Scanner/DirectoryScanner.php +++ b/src/Scanner/DirectoryScanner.php @@ -62,10 +62,9 @@ public function __construct($directory = null) /** * @param DirectoryScanner|string $directory - * @return void * @throws Exception\InvalidArgumentException */ - public function addDirectory($directory) + public function addDirectory($directory) : void { if ($directory instanceof DirectoryScanner) { $this->directories[] = $directory; @@ -85,28 +84,17 @@ public function addDirectory($directory) } } - /** - * @param DirectoryScanner $directoryScanner - * @return void - */ - public function addDirectoryScanner(DirectoryScanner $directoryScanner) + public function addDirectoryScanner(DirectoryScanner $directoryScanner) : void { $this->addDirectory($directoryScanner); } - /** - * @param FileScanner $fileScanner - * @return void - */ - public function addFileScanner(FileScanner $fileScanner) + public function addFileScanner(FileScanner $fileScanner) : void { $this->fileScanners[] = $fileScanner; } - /** - * @return void - */ - protected function scan() + protected function scan() : void { if ($this->isScanned) { return; @@ -140,11 +128,7 @@ public function getNamespaces() // @todo } - /** - * @param bool $returnFileScanners - * @return array - */ - public function getFiles($returnFileScanners = false) + public function getFiles(bool $returnFileScanners = false) : array { $this->scan(); @@ -156,10 +140,7 @@ public function getFiles($returnFileScanners = false) return $return; } - /** - * @return array - */ - public function getClassNames() + public function getClassNames() : array { $this->scan(); @@ -170,11 +151,7 @@ public function getClassNames() return array_keys($this->classToFileScanner); } - /** - * @param bool $returnDerivedScannerClass - * @return array - */ - public function getClasses($returnDerivedScannerClass = false) + public function getClasses(bool $returnDerivedScannerClass = false) : array { $this->scan(); @@ -194,11 +171,7 @@ public function getClasses($returnDerivedScannerClass = false) return $returnClasses; } - /** - * @param string $class - * @return bool - */ - public function hasClass($class) + public function hasClass(string $class) : bool { $this->scan(); @@ -210,12 +183,9 @@ public function hasClass($class) } /** - * @param string $class - * @param bool $returnDerivedScannerClass - * @return ClassScanner|DerivedClassScanner * @throws Exception\InvalidArgumentException */ - public function getClass($class, $returnDerivedScannerClass = false) + public function getClass(string $class, bool $returnDerivedScannerClass = false) : ClassScanner { $this->scan(); @@ -243,7 +213,7 @@ public function getClass($class, $returnDerivedScannerClass = false) * * @return void */ - protected function createClassToFileScannerCache() + protected function createClassToFileScannerCache() : void { if ($this->classToFileScanner !== null) { return; @@ -275,7 +245,7 @@ public static function export() * * @todo implement method */ - public function __toString() + public function __toString() : string { // @todo } diff --git a/src/Scanner/DocBlockScanner.php b/src/Scanner/DocBlockScanner.php index 80b9ac22..a9ad0544 100644 --- a/src/Scanner/DocBlockScanner.php +++ b/src/Scanner/DocBlockScanner.php @@ -71,7 +71,7 @@ class DocBlockScanner implements ScannerInterface * @param string $docComment * @param null|NameInformation $nameInformation */ - public function __construct($docComment, NameInformation $nameInformation = null) + public function __construct(string $docComment, ?NameInformation $nameInformation = null) { $this->docComment = $docComment; $this->nameInformation = $nameInformation; @@ -80,7 +80,7 @@ public function __construct($docComment, NameInformation $nameInformation = null /** * @return string */ - public function getShortDescription() + public function getShortDescription() : ?string { $this->scan(); @@ -90,7 +90,7 @@ public function getShortDescription() /** * @return string */ - public function getLongDescription() + public function getLongDescription() : ?string { $this->scan(); @@ -100,7 +100,7 @@ public function getLongDescription() /** * @return array */ - public function getTags() + public function getTags() : array { $this->scan(); @@ -110,7 +110,7 @@ public function getTags() /** * @return array */ - public function getAnnotations() + public function getAnnotations() : array { $this->scan(); @@ -120,7 +120,7 @@ public function getAnnotations() /** * @return void */ - protected function scan() + protected function scan() : void { if ($this->isScanned) { return; @@ -162,10 +162,10 @@ protected function scan() } //gotos no break needed case 'DOCBLOCK_TAG': - array_push($this->tags, [ + $this->tags[] = [ 'name' => $token[1], 'value' => '', - ]); + ]; end($this->tags); $tagIndex = key($this->tags); $mode = 3; @@ -189,10 +189,7 @@ protected function scan() $this->isScanned = true; } - /** - * @return array - */ - protected function tokenize() + protected function tokenize() : array { static $CONTEXT_INSIDE_DOCBLOCK = 0x01; static $CONTEXT_INSIDE_ASTERISK = 0x02; @@ -254,9 +251,6 @@ protected function tokenize() $MACRO_TOKEN_APPEND_WORD = function () use (&$currentWord, &$tokens, &$tokenIndex) { $tokens[$tokenIndex][1] .= $currentWord; }; - $MACRO_TOKEN_APPEND_WORD_PARTIAL = function ($length) use (&$currentWord, &$tokens, &$tokenIndex) { - $tokens[$tokenIndex][1] .= substr($currentWord, 0, $length); - }; $MACRO_TOKEN_APPEND_LINE = function () use (&$currentLine, &$tokens, &$tokenIndex) { $tokens[$tokenIndex][1] .= $currentLine; }; @@ -291,7 +285,7 @@ protected function tokenize() if ($currentChar === ' ' || $currentChar === "\t") { $MACRO_TOKEN_SET_TYPE( - $context & $CONTEXT_INSIDE_ASTERISK + ($context & $CONTEXT_INSIDE_ASTERISK) ? 'DOCBLOCK_WHITESPACE' : 'DOCBLOCK_WHITESPACE_INDENT' ); diff --git a/src/Scanner/FileScanner.php b/src/Scanner/FileScanner.php index 66cf8e8e..fef139da 100644 --- a/src/Scanner/FileScanner.php +++ b/src/Scanner/FileScanner.php @@ -17,7 +17,7 @@ use function sprintf; use function token_get_all; -class FileScanner extends TokenArrayScanner implements ScannerInterface +class FileScanner extends TokenArrayScanner { /** * @var string @@ -29,7 +29,7 @@ class FileScanner extends TokenArrayScanner implements ScannerInterface * @param null|AnnotationManager $annotationManager * @throws Exception\InvalidArgumentException */ - public function __construct($file, AnnotationManager $annotationManager = null) + public function __construct(string $file, AnnotationManager $annotationManager = null) { $this->file = $file; if (! file_exists($file)) { @@ -41,10 +41,7 @@ public function __construct($file, AnnotationManager $annotationManager = null) parent::__construct(token_get_all(file_get_contents($file)), $annotationManager); } - /** - * @return string - */ - public function getFile() + public function getFile() : string { return $this->file; } diff --git a/src/Scanner/MethodScanner.php b/src/Scanner/MethodScanner.php index 05c67412..2efa3cdf 100644 --- a/src/Scanner/MethodScanner.php +++ b/src/Scanner/MethodScanner.php @@ -119,68 +119,45 @@ public function __construct(array $methodTokens, NameInformation $nameInformatio $this->nameInformation = $nameInformation; } - /** - * @param string $class - * @return MethodScanner - */ - public function setClass($class) + public function setClass(string $class) : self { - $this->class = (string) $class; + $this->class = $class; return $this; } - /** - * @param ClassScanner $scannerClass - * @return MethodScanner - */ - public function setScannerClass(ClassScanner $scannerClass) + public function setScannerClass(ClassScanner $scannerClass) : self { $this->scannerClass = $scannerClass; return $this; } - /** - * @return ClassScanner - */ - public function getClassScanner() + public function getClassScanner() : ?ClassScanner { return $this->scannerClass; } - /** - * @return string - */ - public function getName() + public function getName() : ?string { $this->scan(); return $this->name; } - /** - * @return int - */ - public function getLineStart() + public function getLineStart() : int { $this->scan(); return $this->lineStart; } - /** - * @return int - */ - public function getLineEnd() + public function getLineEnd() : int { $this->scan(); return $this->lineEnd; } - /** - * @return string - */ - public function getDocComment() + public function getDocComment() : ?string { $this->scan(); @@ -189,7 +166,7 @@ public function getDocComment() /** * @param AnnotationManager $annotationManager - * @return AnnotationScanner + * @return AnnotationScanner|bool */ public function getAnnotations(AnnotationManager $annotationManager) { @@ -200,60 +177,42 @@ public function getAnnotations(AnnotationManager $annotationManager) return new AnnotationScanner($annotationManager, $docComment, $this->nameInformation); } - /** - * @return bool - */ - public function isFinal() + public function isFinal() : bool { $this->scan(); return $this->isFinal; } - /** - * @return bool - */ - public function isAbstract() + public function isAbstract() : bool { $this->scan(); return $this->isAbstract; } - /** - * @return bool - */ - public function isPublic() + public function isPublic() : bool { $this->scan(); return $this->isPublic; } - /** - * @return bool - */ - public function isProtected() + public function isProtected() : bool { $this->scan(); return $this->isProtected; } - /** - * @return bool - */ - public function isPrivate() + public function isPrivate() : bool { $this->scan(); return $this->isPrivate; } - /** - * @return bool - */ - public function isStatic() + public function isStatic() : bool { $this->scan(); @@ -263,11 +222,8 @@ public function isStatic() /** * Override the given name for a method, this is necessary to * support traits. - * - * @param string $name - * @return self */ - public function setName($name) + public function setName(string $name) : self { $this->name = $name; return $this; @@ -278,10 +234,9 @@ public function setName($name) * Needed to support traits * * @param int $visibility T_PUBLIC | T_PRIVATE | T_PROTECTED - * @return self - * @throws \Zend\Code\Exception + * @throws Exception\InvalidArgumentException */ - public function setVisibility($visibility) + public function setVisibility(int $visibility) : self { switch (strtolower($visibility)) { case T_PUBLIC: @@ -303,25 +258,18 @@ public function setVisibility($visibility) break; default: - throw new Exception('Invalid visibility argument passed to setVisibility.'); + throw new Exception\InvalidArgumentException('Invalid visibility argument passed to setVisibility.'); } return $this; } - /** - * @return int - */ - public function getNumberOfParameters() + public function getNumberOfParameters() : int { return count($this->getParameters()); } - /** - * @param bool $returnScanner - * @return array - */ - public function getParameters($returnScanner = false) + public function getParameters(bool $returnScanner = false) : array { $this->scan(); @@ -344,16 +292,15 @@ public function getParameters($returnScanner = false) /** * @param int|string $parameterNameOrInfoIndex - * @return ParameterScanner * @throws Exception\InvalidArgumentException */ - public function getParameter($parameterNameOrInfoIndex) + public function getParameter($parameterNameOrInfoIndex) : ParameterScanner { $this->scan(); if (is_int($parameterNameOrInfoIndex)) { $info = $this->infos[$parameterNameOrInfoIndex]; - if ($info['type'] != 'parameter') { + if ($info['type'] !== 'parameter') { throw new Exception\InvalidArgumentException('Index of info offset is not about a parameter'); } } elseif (is_string($parameterNameOrInfoIndex)) { @@ -384,7 +331,7 @@ public function getParameter($parameterNameOrInfoIndex) /** * @return string */ - public function getBody() + public function getBody() : ?string { $this->scan(); @@ -396,7 +343,7 @@ public static function export() // @todo } - public function __toString() + public function __toString() : string { $this->scan(); @@ -451,12 +398,12 @@ protected function scan() if (is_string($token)) { $tokenType = null; $tokenContent = $token; - $tokenLine = $tokenLine + substr_count( + $tokenLine += substr_count( $lastTokenArray[1], "\n" ); // adjust token line by last known newline count } else { - list($tokenType, $tokenContent, $tokenLine) = $token; + [$tokenType, $tokenContent, $tokenLine] = $token; } return $tokenIndex; @@ -575,7 +522,6 @@ protected function scan() if ($infos) { $MACRO_INFO_ADVANCE(); } - $context = 'body'; } goto SCANNER_CONTINUE_BODY; // goto (no break needed); diff --git a/src/Scanner/ParameterScanner.php b/src/Scanner/ParameterScanner.php index 5082bc6d..c8d50889 100644 --- a/src/Scanner/ParameterScanner.php +++ b/src/Scanner/ParameterScanner.php @@ -105,67 +105,32 @@ public function __construct(array $parameterTokens, NameInformation $nameInforma $this->nameInformation = $nameInformation; } - /** - * Set declaring class - * - * @param string $class - * @return void - */ - public function setDeclaringClass($class) + public function setDeclaringClass(string $class) : void { - $this->declaringClass = (string) $class; + $this->declaringClass = $class; } - /** - * Set declaring scanner class - * - * @param ClassScanner $scannerClass - * @return void - */ - public function setDeclaringScannerClass(ClassScanner $scannerClass) + public function setDeclaringScannerClass(ClassScanner $scannerClass) : void { $this->declaringScannerClass = $scannerClass; } - /** - * Set declaring function - * - * @param string $function - * @return void - */ - public function setDeclaringFunction($function) + public function setDeclaringFunction(string $function) : void { $this->declaringFunction = $function; } - /** - * Set declaring scanner function - * - * @param MethodScanner $scannerFunction - * @return void - */ - public function setDeclaringScannerFunction(MethodScanner $scannerFunction) + public function setDeclaringScannerFunction(MethodScanner $scannerFunction) : void { $this->declaringScannerFunction = $scannerFunction; } - /** - * Set position - * - * @param int $position - * @return void - */ - public function setPosition($position) + public function setPosition(int $position) : void { $this->position = $position; } - /** - * Scan - * - * @return void - */ - protected function scan() + protected function scan() : void { if ($this->isScanned) { return; @@ -221,136 +186,76 @@ protected function scan() $this->isScanned = true; } - /** - * Get declaring scanner class - * - * @return ClassScanner - */ - public function getDeclaringScannerClass() + public function getDeclaringScannerClass() : ?ClassScanner { return $this->declaringScannerClass; } - /** - * Get declaring class - * - * @return string - */ - public function getDeclaringClass() + public function getDeclaringClass() : ?string { return $this->declaringClass; } - /** - * Get declaring scanner function - * - * @return MethodScanner - */ - public function getDeclaringScannerFunction() + public function getDeclaringScannerFunction() : ?MethodScanner { return $this->declaringScannerFunction; } - /** - * Get declaring function - * - * @return string - */ - public function getDeclaringFunction() + public function getDeclaringFunction() : ?string { return $this->declaringFunction; } - /** - * Get default value - * - * @return string - */ - public function getDefaultValue() + public function getDefaultValue() : ?string { $this->scan(); return $this->defaultValue; } - /** - * Get class - * - * @return string - */ - public function getClass() + public function getClass() : ?string { $this->scan(); return $this->class; } - /** - * Get name - * - * @return string - */ - public function getName() + public function getName() : ?string { $this->scan(); return $this->name; } - /** - * Get position - * - * @return int - */ - public function getPosition() + public function getPosition() : ?int { $this->scan(); return $this->position; } - /** - * Check if is array - * - * @return bool - */ - public function isArray() + public function isArray() : bool { $this->scan(); return $this->isArray; } - /** - * Check if default value is available - * - * @return bool - */ - public function isDefaultValueAvailable() + public function isDefaultValueAvailable() : bool { $this->scan(); return $this->isDefaultValueAvailable; } - /** - * Check if is optional - * - * @return bool - */ - public function isOptional() + public function isOptional() : bool { $this->scan(); return $this->isOptional; } - /** - * Check if is passed by reference - * - * @return bool - */ - public function isPassedByReference() + public function isPassedByReference() : bool { $this->scan(); diff --git a/src/Scanner/PropertyScanner.php b/src/Scanner/PropertyScanner.php index b11dd9b3..e0d69da1 100644 --- a/src/Scanner/PropertyScanner.php +++ b/src/Scanner/PropertyScanner.php @@ -113,97 +113,64 @@ public function __construct(array $propertyTokens, NameInformation $nameInformat $this->nameInformation = $nameInformation; } - /** - * @param string $class - */ - public function setClass($class) + public function setClass(string $class) : void { $this->class = $class; } - /** - * @param ClassScanner $scannerClass - */ - public function setScannerClass(ClassScanner $scannerClass) + public function setScannerClass(ClassScanner $scannerClass) : void { $this->scannerClass = $scannerClass; } - /** - * @return ClassScanner - */ - public function getClassScanner() + public function getClassScanner() : ?ClassScanner { return $this->scannerClass; } - /** - * @return string - */ - public function getName() + public function getName() : ?string { $this->scan(); return $this->name; } - /** - * @return string - */ - public function getValueType() + public function getValueType() : ?string { $this->scan(); return $this->valueType; } - /** - * @return bool - */ - public function isPublic() + public function isPublic() : bool { $this->scan(); return $this->isPublic; } - /** - * @return bool - */ - public function isPrivate() + public function isPrivate() : bool { $this->scan(); return $this->isPrivate; } - /** - * @return bool - */ - public function isProtected() + public function isProtected() : bool { $this->scan(); return $this->isProtected; } - /** - * @return bool - */ - public function isStatic() + public function isStatic() : bool { $this->scan(); return $this->isStatic; } - /** - * @return string - */ - public function getValue() + public function getValue() : ?string { $this->scan(); return $this->value; } - /** - * @return string - */ - public function getDocComment() + public function getDocComment() : ?string { $this->scan(); return $this->docComment; @@ -211,7 +178,7 @@ public function getDocComment() /** * @param Annotation\AnnotationManager $annotationManager - * @return AnnotationScanner + * @return AnnotationScanner|bool */ public function getAnnotations(Annotation\AnnotationManager $annotationManager) { @@ -225,7 +192,7 @@ public function getAnnotations(Annotation\AnnotationManager $annotationManager) /** * @return string */ - public function __toString() + public function __toString() : string { $this->scan(); return var_export($this, true); @@ -236,7 +203,7 @@ public function __toString() * * @throws \Zend\Code\Exception\RuntimeException */ - protected function scan() + protected function scan() : void { if ($this->isScanned) { return; @@ -258,7 +225,7 @@ protected function scan() foreach ($tokens as $token) { $tempValue = $token; if (! is_string($token)) { - list($tokenType, $tokenContent, $tokenLine) = $token; + [$tokenType, $tokenContent] = $token; switch ($tokenType) { case T_DOC_COMMENT: @@ -316,7 +283,7 @@ protected function scan() $this->valueType = self::T_INTEGER; } elseif (0 === strpos($value, 'array') || 0 === strpos($value, '[')) { $this->valueType = self::T_ARRAY; - } elseif (substr($value, 0, 1) === '"' || substr($value, 0, 1) === "'") { + } elseif (in_array(substr($value, 0, 1), ['"', '\''], true)) { $value = substr($value, 1, -1); // Remove quotes $this->valueType = self::T_STRING; } diff --git a/src/Scanner/TokenArrayScanner.php b/src/Scanner/TokenArrayScanner.php index 9fa199bc..d209040e 100644 --- a/src/Scanner/TokenArrayScanner.php +++ b/src/Scanner/TokenArrayScanner.php @@ -52,11 +52,7 @@ class TokenArrayScanner implements ScannerInterface */ protected $annotationManager; - /** - * @param null|array $tokens - * @param null|AnnotationManager $annotationManager - */ - public function __construct($tokens, AnnotationManager $annotationManager = null) + public function __construct(?array $tokens, ?AnnotationManager $annotationManager = null) { $this->tokens = $tokens; $this->annotationManager = $annotationManager; @@ -65,7 +61,7 @@ public function __construct($tokens, AnnotationManager $annotationManager = null /** * @return AnnotationManager */ - public function getAnnotationManager() + public function getAnnotationManager() : ?AnnotationManager { return $this->annotationManager; } @@ -75,31 +71,28 @@ public function getAnnotationManager() * * @todo Assignment of $this->docComment should probably be done in scan() * and then $this->getDocComment() just retrieves it. - * - * @return string */ - public function getDocComment() + public function getDocComment() : ?string { - foreach ($this->tokens as $token) { - $type = $token[0]; - $value = $token[1]; + foreach ($this->tokens as [$type, $value]) { if (($type == T_OPEN_TAG) || ($type == T_WHITESPACE)) { continue; - } elseif ($type == T_DOC_COMMENT) { + } + + if ($type == T_DOC_COMMENT) { $this->docComment = $value; return $this->docComment; - } else { - // Only whitespace is allowed before file docblocks - return; } + + // Only whitespace is allowed before file docblocks + return null; } + + return null; } - /** - * @return array - */ - public function getNamespaces() + public function getNamespaces() : array { $this->scan(); @@ -113,30 +106,20 @@ public function getNamespaces() return $namespaces; } - /** - * @param null|string $namespace - * @return array|null - */ - public function getUses($namespace = null) + public function getUses(?string $namespace = null) : ?array { $this->scan(); return $this->getUsesNoScan($namespace); } - /** - * @return void - */ public function getIncludes() { $this->scan(); // @todo Implement getIncludes() in TokenArrayScanner } - /** - * @return array - */ - public function getClassNames() + public function getClassNames() : array { $this->scan(); @@ -155,7 +138,7 @@ public function getClassNames() /** * @return ClassScanner[] */ - public function getClasses() + public function getClasses() : array { $this->scan(); @@ -176,7 +159,7 @@ public function getClasses() * * @param string|int $name * @throws Exception\InvalidArgumentException - * @return ClassScanner + * @return ClassScanner|bool */ public function getClass($name) { @@ -232,16 +215,13 @@ public function getClassNameInformation($className) } if (! isset($info)) { - return; + return null; } return new NameInformation($info['namespace'], $info['uses']); } - /** - * @return array - */ - public function getFunctionNames() + public function getFunctionNames() : array { $this->scan(); $functionNames = []; @@ -254,21 +234,12 @@ public function getFunctionNames() return $functionNames; } - /** - * @return array - */ - public function getFunctions() + public function getFunctions() : array { $this->scan(); - $functions = []; -// foreach ($this->infos as $info) { -// if ($info['type'] == 'function') { -// // @todo $functions[] = new FunctionScanner($info['name']); -// } -// } - - return $functions; + // @TODO + return []; } /** @@ -281,7 +252,7 @@ public static function export($tokens) // @todo } - public function __toString() + public function __toString() : string { // @todo } @@ -346,7 +317,7 @@ protected function scan() } $token = $tokens[$tokenIndex]; if (is_array($token)) { - list($tokenType, $tokenContent, $tokenLine) = $token; + [$tokenType, $tokenContent, $tokenLine] = $token; } else { $tokenType = null; $tokenContent = $token; @@ -549,8 +520,6 @@ protected function scan() $infos[$infoIndex]['path'] .= $tokenContent; - SCANNER_INCLUDE_CONTINUE: - if ($MACRO_TOKEN_ADVANCE() === false) { goto SCANNER_END; } @@ -614,8 +583,6 @@ protected function scan() } } - SCANNER_CLASS_CONTINUE: - if ($MACRO_TOKEN_ADVANCE() === false) { goto SCANNER_END; } @@ -643,13 +610,7 @@ protected function scan() $this->isScanned = true; } - /** - * Check for namespace - * - * @param string $namespace - * @return bool - */ - public function hasNamespace($namespace) + public function hasNamespace(string $namespace) : bool { $this->scan(); @@ -662,11 +623,9 @@ public function hasNamespace($namespace) } /** - * @param string $namespace - * @return void|array * @throws Exception\InvalidArgumentException */ - protected function getUsesNoScan($namespace) + protected function getUsesNoScan(?string $namespace) : ?array { $namespaces = []; foreach ($this->infos as $info) { @@ -680,7 +639,7 @@ protected function getUsesNoScan($namespace) } elseif (! is_string($namespace)) { throw new Exception\InvalidArgumentException('Invalid namespace provided'); } elseif (! in_array($namespace, $namespaces)) { - return; + return null; } $uses = []; diff --git a/src/Scanner/Util.php b/src/Scanner/Util.php index 764060c2..53cfc7ba 100644 --- a/src/Scanner/Util.php +++ b/src/Scanner/Util.php @@ -33,10 +33,9 @@ class Util * @param string $value * @param null|string $key * @param \stdClass $data - * @return void * @throws Exception\InvalidArgumentException */ - public static function resolveImports(&$value, $key = null, stdClass $data = null) + public static function resolveImports(&$value, $key = null, stdClass $data = null) : void { if (! is_object($data) || ! property_exists($data, 'uses') diff --git a/test/Generator/AbstractGeneratorTest.php b/test/Generator/AbstractGeneratorTest.php index 06b42219..3c822411 100644 --- a/test/Generator/AbstractGeneratorTest.php +++ b/test/Generator/AbstractGeneratorTest.php @@ -31,10 +31,4 @@ public function testConstructor() self::assertInstanceOf(GeneratorInterface::class, $generator); self::assertEquals('foo', $generator->getIndentation()); } - - public function testSetOptionsThrowsExceptionOnInvalidArgument() - { - $this->expectException(InvalidArgumentException::class); - $this->getMockForAbstractClass(AbstractGenerator::class, ['sss']); - } } diff --git a/test/Generator/ClassGeneratorTest.php b/test/Generator/ClassGeneratorTest.php index 72a512b3..87198379 100644 --- a/test/Generator/ClassGeneratorTest.php +++ b/test/Generator/ClassGeneratorTest.php @@ -149,9 +149,8 @@ public function testSetPropertyNoArrayOrPropertyThrowsException() { $classGenerator = new ClassGenerator(); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Zend\Code\Generator\ClassGenerator::addProperty expects string for name'); - $classGenerator->addProperty(true); + $this->expectException(\TypeError::class); + $classGenerator->addProperty(new \stdClass()); } public function testMethodAccessors() @@ -179,10 +178,9 @@ public function testSetMethodNoMethodOrArrayThrowsException() { $classGenerator = new ClassGenerator(); - $this->expectException(ExceptionInterface::class); - $this->expectExceptionMessage('Zend\Code\Generator\ClassGenerator::addMethod expects string for name'); + $this->expectException(\TypeError::class); - $classGenerator->addMethod(true); + $classGenerator->addMethod(new \stdClass()); } public function testSetMethodNameAlreadyExistsThrowsException() @@ -652,7 +650,7 @@ public function testAddConstantThrowsExceptionWithInvalidName() { $classGenerator = new ClassGenerator(); - $this->expectException(InvalidArgumentException::class); + $this->expectException(\TypeError::class); $classGenerator->addConstant([], 'value1'); } diff --git a/test/Generator/TraitGeneratorTest.php b/test/Generator/TraitGeneratorTest.php index a838585f..5f2f8eb0 100644 --- a/test/Generator/TraitGeneratorTest.php +++ b/test/Generator/TraitGeneratorTest.php @@ -105,9 +105,8 @@ public function testSetPropertyNoArrayOrPropertyThrowsException() { $classGenerator = new TraitGenerator(); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Zend\Code\Generator\TraitGenerator::addProperty expects string for name'); - $classGenerator->addProperty(true); + $this->expectException(\TypeError::class); + $classGenerator->addProperty(new \stdClass()); } public function testMethodAccessors() @@ -135,10 +134,9 @@ public function testSetMethodNoMethodOrArrayThrowsException() { $classGenerator = new TraitGenerator(); - $this->expectException(ExceptionInterface::class); - $this->expectExceptionMessage('Zend\Code\Generator\TraitGenerator::addMethod expects string for name'); + $this->expectException(\TypeError::class); - $classGenerator->addMethod(true); + $classGenerator->addMethod(new \stdClass()); } public function testSetMethodNameAlreadyExistsThrowsException() diff --git a/test/Reflection/TestAsset/InjectableClassReflection.php b/test/Reflection/TestAsset/InjectableClassReflection.php index 59a4af09..ffc30b48 100644 --- a/test/Reflection/TestAsset/InjectableClassReflection.php +++ b/test/Reflection/TestAsset/InjectableClassReflection.php @@ -3,17 +3,21 @@ namespace ZendTest\Code\Reflection\TestAsset; use Zend\Code\Reflection\ClassReflection; +use Zend\Code\Scanner\FileScanner; class InjectableClassReflection extends ClassReflection { + /** + * @var FileScanner|null + */ protected $fileScanner; - public function setFileScanner($fileScanner) + public function setFileScanner(FileScanner $fileScanner) : void { $this->fileScanner = $fileScanner; } - protected function createFileScanner($filename) + protected function createFileScanner(string $filename) : FileScanner { return $this->fileScanner; } diff --git a/test/Reflection/TestAsset/InjectableMethodReflection.php b/test/Reflection/TestAsset/InjectableMethodReflection.php index a1da8b02..d336fa0b 100644 --- a/test/Reflection/TestAsset/InjectableMethodReflection.php +++ b/test/Reflection/TestAsset/InjectableMethodReflection.php @@ -3,17 +3,21 @@ namespace ZendTest\Code\Reflection\TestAsset; use Zend\Code\Reflection\MethodReflection; +use Zend\Code\Scanner\FileScanner; class InjectableMethodReflection extends MethodReflection { + /** + * @var FileScanner|null + */ protected $fileScanner; - public function setFileScanner($fileScanner) + public function setFileScanner(FileScanner $fileScanner) { $this->fileScanner = $fileScanner; } - protected function createFileScanner($filename) + protected function createFileScanner(string $filename) : FileScanner { return $this->fileScanner; } diff --git a/test/Reflection/TestAsset/InjectablePropertyReflection.php b/test/Reflection/TestAsset/InjectablePropertyReflection.php index ffb43953..383857db 100644 --- a/test/Reflection/TestAsset/InjectablePropertyReflection.php +++ b/test/Reflection/TestAsset/InjectablePropertyReflection.php @@ -3,17 +3,21 @@ namespace ZendTest\Code\Reflection\TestAsset; use Zend\Code\Reflection\PropertyReflection; +use Zend\Code\Scanner\FileScanner; class InjectablePropertyReflection extends PropertyReflection { + /** + * @var FileScanner|null + */ protected $fileScanner; - public function setFileScanner($fileScanner) + public function setFileScanner(FileScanner $fileScanner) : void { $this->fileScanner = $fileScanner; } - protected function createFileScanner($filename) + protected function createFileScanner(string $filename) : FileScanner { return $this->fileScanner; }