Skip to content

Commit

Permalink
Add parameter type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Dec 17, 2022
1 parent e29955f commit c20b3c3
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 82 deletions.
3 changes: 3 additions & 0 deletions UPGRADE.md
Expand Up @@ -4,3 +4,6 @@
`Doctrine\Common\Lexer\Token` objects. When using `doctrine/lexer` 2, these
implement `ArrayAccess` as a way for you to still be able to treat them as
arrays in some ways.
Parameter type declarations have been added to all methods of all classes. If
you have classes inheriting from classes inside this package, you should add
parameter and return type declarations.
4 changes: 2 additions & 2 deletions lib/Doctrine/Common/Annotations/Annotation.php
Expand Up @@ -33,7 +33,7 @@ final public function __construct(array $data)
*
* @throws BadMethodCallException
*/
public function __get($name)
public function __get(string $name)
{
throw new BadMethodCallException(
sprintf("Unknown property '%s' on annotation '%s'.", $name, static::class)
Expand All @@ -48,7 +48,7 @@ public function __get($name)
*
* @throws BadMethodCallException
*/
public function __set($name, $value)
public function __set(string $name, $value)
{
throw new BadMethodCallException(
sprintf("Unknown property '%s' on annotation '%s'.", $name, static::class)
Expand Down
65 changes: 28 additions & 37 deletions lib/Doctrine/Common/Annotations/AnnotationException.php
Expand Up @@ -19,23 +19,19 @@ class AnnotationException extends Exception
/**
* Creates a new AnnotationException describing a Syntax error.
*
* @param string $message Exception message
*
* @return AnnotationException
*/
public static function syntaxError($message)
public static function syntaxError(string $message)
{
return new self('[Syntax Error] ' . $message);
}

/**
* Creates a new AnnotationException describing a Semantical error.
*
* @param string $message Exception message
*
* @return AnnotationException
*/
public static function semanticalError($message)
public static function semanticalError(string $message)
{
return new self('[Semantical Error] ' . $message);
}
Expand All @@ -44,36 +40,29 @@ public static function semanticalError($message)
* Creates a new AnnotationException describing an error which occurred during
* the creation of the annotation.
*
* @param string $message
*
* @return AnnotationException
*/
public static function creationError($message, ?Throwable $previous = null)
public static function creationError(string $message, ?Throwable $previous = null)
{
return new self('[Creation Error] ' . $message, 0, $previous);
}

/**
* Creates a new AnnotationException describing a type error.
*
* @param string $message
*
* @return AnnotationException
*/
public static function typeError($message)
public static function typeError(string $message)
{
return new self('[Type Error] ' . $message);
}

/**
* Creates a new AnnotationException describing a constant semantical error.
*
* @param string $identifier
* @param string $context
*
* @return AnnotationException
*/
public static function semanticalErrorConstants($identifier, $context = null)
public static function semanticalErrorConstants(string $identifier, ?string $context = null)
{
return self::semanticalError(sprintf(
"Couldn't find constant %s%s.",
Expand All @@ -85,16 +74,17 @@ public static function semanticalErrorConstants($identifier, $context = null)
/**
* Creates a new AnnotationException describing an type error of an attribute.
*
* @param string $attributeName
* @param string $annotationName
* @param string $context
* @param string $expected
* @param mixed $actual
* @param mixed $actual
*
* @return AnnotationException
*/
public static function attributeTypeError($attributeName, $annotationName, $context, $expected, $actual)
{
public static function attributeTypeError(
string $attributeName,
string $annotationName,
string $context,
string $expected,
$actual
) {
return self::typeError(sprintf(
'Attribute "%s" of @%s declared on %s expects %s, but got %s.',
$attributeName,
Expand All @@ -108,15 +98,14 @@ public static function attributeTypeError($attributeName, $annotationName, $cont
/**
* Creates a new AnnotationException describing an required error of an attribute.
*
* @param string $attributeName
* @param string $annotationName
* @param string $context
* @param string $expected
*
* @return AnnotationException
*/
public static function requiredError($attributeName, $annotationName, $context, $expected)
{
public static function requiredError(
string $attributeName,
string $annotationName,
string $context,
string $expected
) {
return self::typeError(sprintf(
'Attribute "%s" of @%s declared on %s expects %s. This value should not be null.',
$attributeName,
Expand All @@ -129,16 +118,18 @@ public static function requiredError($attributeName, $annotationName, $context,
/**
* Creates a new AnnotationException describing a invalid enummerator.
*
* @param string $attributeName
* @param string $annotationName
* @param string $context
* @param mixed $given
* @phpstan-param list<string> $available
* @param mixed $given
* @phpstan-param list<string> $available
*
* @return AnnotationException
*/
public static function enumeratorError($attributeName, $annotationName, $context, $available, $given)
{
public static function enumeratorError(
string $attributeName,
string $annotationName,
string $context,
array $available,
$given
) {
return new self(sprintf(
'[Enum Error] Attribute "%s" of @%s declared on %s accepts only [%s], but got %s.',
$attributeName,
Expand Down
9 changes: 3 additions & 6 deletions lib/Doctrine/Common/Annotations/CachedReader.php
Expand Up @@ -183,12 +183,11 @@ private function fetchFromCache($cacheKey, ReflectionClass $class)
/**
* Saves a value to the cache.
*
* @param string $cacheKey The cache key.
* @param mixed $value The value.
* @param mixed $value The value.
*
* @return void
*/
private function saveToCache($cacheKey, $value)
private function saveToCache(string $cacheKey, $value)
{
$this->cache->save($cacheKey, $value);
if (! $this->debug) {
Expand All @@ -201,11 +200,9 @@ private function saveToCache($cacheKey, $value)
/**
* Checks if the cache is fresh.
*
* @param string $cacheKey
*
* @return bool
*/
private function isCacheFresh($cacheKey, ReflectionClass $class)
private function isCacheFresh(string $cacheKey, ReflectionClass $class)
{
$lastModification = $this->getLastModification($class);
if ($lastModification === 0) {
Expand Down
21 changes: 6 additions & 15 deletions lib/Doctrine/Common/Annotations/DocParser.php
Expand Up @@ -286,33 +286,29 @@ public function setIgnoredAnnotationNames(array $names)
*
* @return void
*/
public function setIgnoredAnnotationNamespaces($ignoredAnnotationNamespaces)
public function setIgnoredAnnotationNamespaces(array $ignoredAnnotationNamespaces)
{
$this->ignoredAnnotationNamespaces = $ignoredAnnotationNamespaces;
}

/**
* Sets ignore on not-imported annotations.
*
* @param bool $bool
*
* @return void
*/
public function setIgnoreNotImportedAnnotations($bool)
public function setIgnoreNotImportedAnnotations(bool $bool)
{
$this->ignoreNotImportedAnnotations = (bool) $bool;
$this->ignoreNotImportedAnnotations = $bool;
}

/**
* Sets the default namespaces.
*
* @param string $namespace
*
* @return void
*
* @throws RuntimeException
*/
public function addNamespace($namespace)
public function addNamespace(string $namespace)
{
if ($this->imports) {
throw new RuntimeException('You must either use addNamespace(), or setImports(), but not both.');
Expand Down Expand Up @@ -342,27 +338,22 @@ public function setImports(array $imports)
/**
* Sets current target context as bitmask.
*
* @param int $target
*
* @return void
*/
public function setTarget($target)
public function setTarget(int $target)
{
$this->target = $target;
}

/**
* Parses the given docblock string for annotations.
*
* @param string $input The docblock string to parse.
* @param string $context The parsing context.
*
* @phpstan-return list<object> Array of annotations. If no annotations are found, an empty array is returned.
*
* @throws AnnotationException
* @throws ReflectionException
*/
public function parse($input, $context = '')
public function parse(string $input, string $context = '')
{
$pos = $this->findInitialTokenPosition($input);
if ($pos === null) {
Expand Down
21 changes: 9 additions & 12 deletions lib/Doctrine/Common/Annotations/FileCacheReader.php
Expand Up @@ -55,15 +55,13 @@ class FileCacheReader implements Reader
/** @var int */
private $umask;

/**
* @param string $cacheDir
* @param bool $debug
* @param int $umask
*
* @throws InvalidArgumentException
*/
public function __construct(Reader $reader, $cacheDir, $debug = false, $umask = 0002)
{
/** @throws InvalidArgumentException */
public function __construct(
Reader $reader,
string $cacheDir,
bool $debug = false,
int $umask = 0002
) {
if (! is_int($umask)) {
throw new InvalidArgumentException(sprintf(
'The parameter umask must be an integer, was: %s',
Expand Down Expand Up @@ -210,12 +208,11 @@ public function getMethodAnnotations(ReflectionMethod $method)
/**
* Saves the cache file.
*
* @param string $path
* @param mixed $data
* @param mixed $data
*
* @return void
*/
private function saveCacheFile($path, $data)
private function saveCacheFile(string $path, $data)
{
if (! is_writable($this->dir)) {
throw new InvalidArgumentException(sprintf(
Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/Common/Annotations/IndexedReader.php
Expand Up @@ -88,12 +88,11 @@ public function getPropertyAnnotation(ReflectionProperty $property, $annotationN
/**
* Proxies all methods to the delegate.
*
* @param string $method
* @param mixed[] $args
*
* @return mixed
*/
public function __call($method, $args)
public function __call(string $method, $args)
{
return call_user_func_array([$this->delegate, $method], $args);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/Annotations/PhpParser.php
Expand Up @@ -70,7 +70,7 @@ public function parseUseStatements($reflection): array
*
* @return string|null The content of the file or null if the file does not exist.
*/
private function getFileContent($filename, $lineNumber)
private function getFileContent(string $filename, $lineNumber)
{
if (! is_file($filename)) {
return null;
Expand Down
4 changes: 1 addition & 3 deletions lib/Doctrine/Common/Annotations/SimpleAnnotationReader.php
Expand Up @@ -31,11 +31,9 @@ public function __construct()
/**
* Adds a namespace in which we will look for annotations.
*
* @param string $namespace
*
* @return void
*/
public function addNamespace($namespace)
public function addNamespace(string $namespace)
{
$this->parser->addNamespace($namespace);
}
Expand Down
7 changes: 3 additions & 4 deletions lib/Doctrine/Common/Annotations/TokenParser.php
Expand Up @@ -46,8 +46,7 @@ class TokenParser
*/
private $pointer = 0;

/** @param string $contents */
public function __construct($contents)
public function __construct(string $contents)
{
$this->tokens = token_get_all($contents);

Expand All @@ -71,7 +70,7 @@ public function __construct($contents)
*
* @return mixed[]|string|null The token if exists, null otherwise.
*/
public function next($docCommentIsComment = true)
public function next(bool $docCommentIsComment = true)
{
for ($i = $this->pointer; $i < $this->numTokens; $i++) {
$this->pointer++;
Expand Down Expand Up @@ -149,7 +148,7 @@ public function parseUseStatement()
*
* @return array<string, string> A list with all found use statements.
*/
public function parseUseStatements($namespaceName)
public function parseUseStatements(string $namespaceName)
{
$statements = [];
while (($token = $this->next())) {
Expand Down

0 comments on commit c20b3c3

Please sign in to comment.