-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Type hints
- Loading branch information
Richard Quadling
authored and
Richard Quadling
committed
Aug 23, 2019
1 parent
d77f25b
commit dce360b
Showing
22 changed files
with
649 additions
and
565 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,19 +15,31 @@ obtain it through the world-wide-web, please send an email | |
to [email protected] so I can send you a copy immediately. | ||
TXT; | ||
|
||
$rules = array( | ||
$rules = [ | ||
'@PSR2' => true, | ||
'@Symfony' => true, | ||
'concat_space' => false, | ||
'native_function_invocation' => true, | ||
'cast_spaces' => [ | ||
'space' => 'none', | ||
], | ||
'concat_space' => [ | ||
'spacing' => 'none', | ||
], | ||
'native_function_invocation' => [ | ||
'scope' => 'namespaced', | ||
], | ||
'psr4' => true, | ||
'phpdoc_align' => true, | ||
'array_syntax' => ['syntax' => 'short'], | ||
'header_comment' => array( | ||
'phpdoc_align' => [ | ||
'align' => 'left', | ||
], | ||
'array_syntax' => [ | ||
'syntax' => 'short', | ||
], | ||
'header_comment' => [ | ||
'header' => $header, | ||
'commentType' => PhpCsFixer\Fixer\Comment\HeaderCommentFixer::HEADER_PHPDOC, | ||
), | ||
); | ||
], | ||
'yoda_style' => false, | ||
]; | ||
|
||
$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so I can send you a copy immediately. | ||
*/ | ||
use Assert\Assertion; | ||
use Assert\AssertionFailedException; | ||
|
||
/** | ||
* Class MethodDocGenerator. | ||
|
@@ -30,50 +32,50 @@ public function generateChainDocs() | |
|
||
/** | ||
* @param ReflectionMethod[] $methods | ||
* @param string $format | ||
* @param callable|false $skipParameterTest | ||
* @param string $prefix | ||
* @param string $format | ||
* @param callable|false $skipParameterTest | ||
* @param string $prefix | ||
* | ||
* @return array | ||
* | ||
* @throws \Assert\AssertionFailedException | ||
* @throws AssertionFailedException | ||
*/ | ||
private function generateMethodDocs($methods, $format, $skipParameterTest, $prefix = '') | ||
{ | ||
$lines = []; | ||
\asort($methods); | ||
asort($methods); | ||
foreach ($methods as $method) { | ||
$doc = $method->getDocComment(); | ||
list(, $descriptionLine) = \explode("\n", $doc); | ||
$shortDescription = \trim(\substr($descriptionLine, 7), '.'); | ||
$methodName = $prefix.($prefix ? \ucfirst($method->getName()) : $method->getName()); | ||
list(, $descriptionLine) = explode("\n", $doc); | ||
$shortDescription = trim(substr($descriptionLine, 7), '.'); | ||
$methodName = $prefix.($prefix ? ucfirst($method->getName()) : $method->getName()); | ||
|
||
if (\preg_match('`\* This is an alias of {@see (?P<aliasOf>[^\s\}]++)`sim', $doc, $aliasMatch)) { | ||
$shortDescription .= \sprintf('. This is an alias of Assertion::%s()', \trim($aliasMatch['aliasOf'], '(){}')); | ||
if (preg_match('`\* This is an alias of {@see (?P<aliasOf>[^\s\}]++)`sim', $doc, $aliasMatch)) { | ||
$shortDescription .= sprintf('. This is an alias of Assertion::%s()', trim($aliasMatch['aliasOf'], '(){}')); | ||
} | ||
|
||
$parameters = []; | ||
|
||
foreach ($method->getParameters() as $parameterIndex => $methodParameter) { | ||
if ( | ||
(\is_bool($skipParameterTest) && $skipParameterTest) || | ||
(\is_callable($skipParameterTest) && $skipParameterTest($methodParameter)) | ||
(is_bool($skipParameterTest) && $skipParameterTest) || | ||
(is_callable($skipParameterTest) && $skipParameterTest($methodParameter)) | ||
) { | ||
continue; | ||
} | ||
|
||
$parameter = '$'.$methodParameter->getName(); | ||
|
||
$type = \version_compare(PHP_VERSION, '7.0.0') >= 0 ? $methodParameter->getType() : null; | ||
$type = version_compare(PHP_VERSION, '7.0.0') >= 0 ? $methodParameter->getType() : null; | ||
|
||
if (\is_null($type)) { | ||
\preg_match(\sprintf('`\* @param (?P<type>[^ ]++) +\%s\b`sim', $parameter), $doc, $matches); | ||
if (is_null($type)) { | ||
preg_match(sprintf('`\* @param (?P<type>[^ ]++) +\%s\b`sim', $parameter), $doc, $matches); | ||
if (isset($matches['type'])) { | ||
$type = ( | ||
$methodParameter->isOptional() && | ||
null == $methodParameter->getDefaultValue() | ||
) | ||
? \str_replace(['|null', 'null|'], '', $matches['type']) | ||
? str_replace(['|null', 'null|'], '', $matches['type']) | ||
: $matches['type']; | ||
} | ||
} | ||
|
@@ -82,21 +84,21 @@ private function generateMethodDocs($methods, $format, $skipParameterTest, $pref | |
$type .= '|null'; | ||
} | ||
|
||
\Assert\Assertion::notEmpty($type, \sprintf('No type defined for %s in %s', $parameter, $methodName)); | ||
$parameter = \sprintf('%s %s', $type, $parameter); | ||
Assertion::notEmpty($type, sprintf('No type defined for %s in %s', $parameter, $methodName)); | ||
$parameter = sprintf('%s %s', $type, $parameter); | ||
|
||
if ($methodParameter->isOptional()) { | ||
if (null === $methodParameter->getDefaultValue()) { | ||
$parameter .= ' = null'; | ||
} else { | ||
$parameter .= \sprintf(' = \'%s\'', $methodParameter->getDefaultValue()); | ||
$parameter .= sprintf(' = \'%s\'', $methodParameter->getDefaultValue()); | ||
} | ||
} | ||
|
||
$parameters[] = $parameter; | ||
} | ||
|
||
$lines[] = \sprintf($format, $methodName, \implode(', ', $parameters), $shortDescription); | ||
$lines[] = sprintf($format, $methodName, implode(', ', $parameters), $shortDescription); | ||
} | ||
|
||
return $lines; | ||
|
@@ -109,14 +111,14 @@ private function gatherAssertions() | |
{ | ||
$reflClass = new ReflectionClass('Assert\Assertion'); | ||
|
||
return \array_filter( | ||
return array_filter( | ||
$reflClass->getMethods(ReflectionMethod::IS_STATIC), | ||
function (ReflectionMethod $reflMethod) { | ||
if ($reflMethod->isProtected()) { | ||
return false; | ||
} | ||
|
||
if (\in_array($reflMethod->getName(), ['__callStatic', 'createException', 'stringify'])) { | ||
if (in_array($reflMethod->getName(), ['__callStatic', 'createException', 'stringify'])) { | ||
return false; | ||
} | ||
|
||
|
@@ -126,33 +128,33 @@ function (ReflectionMethod $reflMethod) { | |
} | ||
|
||
/** | ||
* @param string $phpFile | ||
* @param string $phpFile | ||
* @param string[] $lines | ||
* @param string $fileType | ||
* @param string $fileType | ||
*/ | ||
private function generateFile($phpFile, $lines, $fileType) | ||
{ | ||
$phpFile = \realpath($phpFile); | ||
$fileContent = \file_get_contents($phpFile); | ||
$phpFile = realpath($phpFile); | ||
$fileContent = file_get_contents($phpFile); | ||
|
||
switch ($fileType) { | ||
case 'class': | ||
$fileContent = \preg_replace( | ||
$fileContent = preg_replace( | ||
'`\* @method.*? \*/\nclass `sim', | ||
\sprintf("%s\n */\nclass ", \trim(\implode("\n", $lines))), | ||
sprintf("%s\n */\nclass ", trim(implode("\n", $lines))), | ||
$fileContent | ||
); | ||
break; | ||
case 'readme': | ||
$fileContent = \preg_replace( | ||
$fileContent = preg_replace( | ||
'/```php\n<\?php\nuse Assert\\\Assertion;\n\nAssertion::.*?```/sim', | ||
\sprintf("```php\n<?php\nuse Assert\\Assertion;\n\n%s\n\n```", \implode("\n", $lines)), | ||
sprintf("```php\n<?php\nuse Assert\\Assertion;\n\n%s\n\n```", implode("\n", $lines)), | ||
$fileContent | ||
); | ||
break; | ||
} | ||
|
||
$writtenBytes = \file_put_contents($phpFile, $fileContent); | ||
$writtenBytes = file_put_contents($phpFile, $fileContent); | ||
|
||
if (false !== $writtenBytes) { | ||
echo 'Generated '.$phpFile.'.'.PHP_EOL; | ||
|
@@ -166,7 +168,7 @@ public function generateAssertionDocs() | |
return false; | ||
}; | ||
|
||
$docs = \array_merge( | ||
$docs = array_merge( | ||
$this->generateMethodDocs($this->gatherAssertions(), ' * @method static bool %s(%s) %s for all values.', $skipParameterTest, 'all'), | ||
$this->generateMethodDocs($this->gatherAssertions(), ' * @method static bool %s(%s) %s or that the value is null.', $skipParameterTest, 'nullOr') | ||
); | ||
|
@@ -178,7 +180,7 @@ public function generateReadMe() | |
{ | ||
$mdFile = __DIR__.'/../README.md'; | ||
$skipParameterTest = function (ReflectionParameter $parameter) { | ||
return \in_array($parameter->getName(), ['message', 'propertyPath', 'encoding']); | ||
return in_array($parameter->getName(), ['message', 'propertyPath', 'encoding']); | ||
}; | ||
|
||
$docs = $this->generateMethodDocs($this->gatherAssertions(), 'Assertion::%s(%s);', $skipParameterTest); | ||
|
@@ -193,7 +195,7 @@ public function generateLazyAssertionDocs() | |
return 0 === $parameter->getPosition(); | ||
}; | ||
|
||
$docs = \array_merge( | ||
$docs = array_merge( | ||
$this->generateMethodDocs($this->gatherAssertions(), ' * @method $this %s(%s) %s.', $skipParameterTest), | ||
$this->generateMethodDocs($this->gatherAssertionChainSwitches(), ' * @method $this %s(%s) %s.', false) | ||
); | ||
|
@@ -208,14 +210,14 @@ private function gatherAssertionChainSwitches() | |
{ | ||
$reflClass = new ReflectionClass('Assert\AssertionChain'); | ||
|
||
return \array_filter( | ||
return array_filter( | ||
$reflClass->getMethods(ReflectionMethod::IS_PUBLIC), | ||
function (ReflectionMethod $reflMethod) { | ||
if (!$reflMethod->isPublic()) { | ||
return false; | ||
} | ||
|
||
if (\in_array($reflMethod->getName(), ['__construct', '__call', 'setAssertionClassName'])) { | ||
if (in_array($reflMethod->getName(), ['__construct', '__call', 'setAssertionClassName'])) { | ||
return false; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so I can send you a copy immediately. | ||
*/ | ||
|
||
require_once __DIR__.'/../vendor/autoload.php'; | ||
require_once __DIR__.'/MethodDocGenerator.php'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.