Skip to content

Commit

Permalink
Merge branch 'hotfix/25'
Browse files Browse the repository at this point in the history
Close #25
  • Loading branch information
michalbundyra committed May 18, 2019
2 parents 5c8d28d + 67b76cd commit 194193c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ All notable changes to this project will be documented in this file, in reverse
- `PHP\CorrectClassNameCase`
- `PHP\DisallowFqn`

- [#25](https://github.com/webimpress/coding-standard/pull/25) fixes type suggestions, allows `self` and `parent` to be used as specification for complex types (like `object` or class/interface)

## 1.0.2 - 2019-05-12

### Added
Expand Down
28 changes: 4 additions & 24 deletions src/WebimpressCodingStandard/Helper/MethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,6 @@ trait MethodsTrait
*/
private $implementedInterfaceNames = [];

/**
* Allowed simple type hints for method params.
*
* @var string[]
*/
private $simpleTypeHints = [
'array',
'bool',
'callable',
'float',
'iterable',
'int',
'object',
'parent',
'resource',
'self',
'string',
];

/**
* @var string[]
*/
Expand All @@ -118,12 +99,8 @@ trait MethodsTrait
'?int',
'object',
'?object',
'parent',
'?parent',
'resource',
'?resource',
'self',
'?self',
'string',
'?string',
'void',
Expand Down Expand Up @@ -218,7 +195,10 @@ private function getSuggestedType(string $class) : string
$suffix = strstr($class, '[');
$clear = strtolower(strtr($class, ['?' => '', '[' => '', ']' => '']));

if (in_array($clear, $this->simpleReturnTypes + ['static' => 'static'], true)) {
$typeMap = $this->simpleReturnTypes
+ ['parent' => 'parent', 'self' => 'self', 'static' => 'static'];

if (in_array($clear, $typeMap, true)) {
return $prefix . $clear . $suffix;
}

Expand Down
22 changes: 22 additions & 0 deletions test/Sniffs/Functions/ParamUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,26 @@ class FunctionParam
?\Generator $d,
?object $e
) {}

/**
* @param self $self
* @param static $static
* @param parent $parent
*/
abstract public function canBeUsedAsClassTypeSpecification(
MyClass $self,
MyClass $static,
MyClass $parent
) : void;

/**
* @param self $self
* @param static $static
* @param parent $parent
*/
abstract public function canBeUsedAsObjectTypeSpecification(
object $self,
object $static,
object $parent
) : void;
}
30 changes: 30 additions & 0 deletions test/Sniffs/Functions/ReturnTypeUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,34 @@ abstract class FunctionCommentReturn
{
return $a[0] * $b <=> $c ? ['a'] : 0.1;
}

/**
* @return self
*/
abstract public function selfCanBeUsedAsClassTypeSpecification() : MyClass;

/**
* @return static
*/
abstract public function staticCanBeUsedAsClassTypeSpecification() : MyClass;

/**
* @return parent
*/
abstract public function parentCanBeUsedAsClassTypeSpecification() : MyClass;

/**
* @return self
*/
abstract public function selfCanBeUsedAsObjectTypeSpecification() : object;

/**
* @return static
*/
abstract public function staticCanBeUsedAsObjectTypeSpecification() : object;

/**
* @return parent
*/
abstract public function parentCanBeUsedAsObjectTypeSpecification() : object;
}

0 comments on commit 194193c

Please sign in to comment.