Skip to content

Commit

Permalink
Fixes regular expression to check class name
Browse files Browse the repository at this point in the history
Regular expression was used in the following sniffs:
- `Commenting\TagWithType`
- `Functions\Param`
- `Functions\ReturnType`

Unit tests included.
  • Loading branch information
michalbundyra committed May 15, 2019
1 parent 75affdf commit 2255ec9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/WebimpressCodingStandard/Helper/MethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ private function isType(string $tag, string $type) : bool
return true;
}

return (bool) preg_match('/^((?:\\\\?[a-z0-9]+)+(?:\[\])*)(\|(?:\\\\?[a-z0-9]+)+(?:\[\])*)*$/i', $type);
$classRegexp = '\\\\?[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*';

return (bool) preg_match(
'/^((?:' . $classRegexp . ')+(?:\[\])*)(\|(?:' . $classRegexp . ')+(?:\[\])*)*$/i',
$type
);
}
}
2 changes: 0 additions & 2 deletions src/WebimpressCodingStandard/Sniffs/Functions/ParamSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use function array_filter;
use function array_merge;
use function count;
use function current;
use function explode;
use function implode;
Expand Down Expand Up @@ -321,7 +320,6 @@ private function checkParam(
}
}

$count = count($types);
$break = false;
foreach ($types as $key => $type) {
$lower = strtolower($type);
Expand Down
14 changes: 11 additions & 3 deletions test/Sniffs/Commenting/TagWithTypeUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

namespace MyNamespace\Test;

class TagWithType {
class TagWithType
{
/** @var \Property_Type */
public $className;

/**
* @throws \Exception
* @param \Param_Type $param
* @return \Return_Type
* @throws \Exception_Name
*/
public function test() {}
public function test(\Param_Type $param) : \Return_Type
{
}
}
7 changes: 6 additions & 1 deletion test/Sniffs/Functions/ParamUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,10 @@ class FunctionParam
/**
* @param int,string $a
*/
public function invalidType($a);
abstract public function invalidType($a);

/**
* @param Other_Param $Foo
*/
public function withUnderscore(With_Underscore $a, $foo = null) {}
}
1 change: 1 addition & 0 deletions test/Sniffs/Functions/ParamUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function getErrorList(string $testFile = '') : array
98 => 1,
130 => 1,
135 => 1,
143 => 2,
];
}

Expand Down
13 changes: 13 additions & 0 deletions test/Sniffs/Functions/ReturnTypeUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,17 @@ abstract class FunctionCommentReturn
{
return $this->thisParent();
}

public function withReturnType($a): \Return_Type
{
return $a;
}

/**
* @return \Return_Type
*/
public function withDocBlock($a)
{
return $a;
}
}

0 comments on commit 2255ec9

Please sign in to comment.