Skip to content

Commit

Permalink
Code styling
Browse files Browse the repository at this point in the history
Type hints
  • Loading branch information
Richard Quadling authored and Richard Quadling committed Aug 23, 2019
1 parent d77f25b commit dce360b
Show file tree
Hide file tree
Showing 22 changed files with 649 additions and 565 deletions.
28 changes: 20 additions & 8 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -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__;

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ Assertion::choice(mixed $value, array $choices);
Assertion::choicesNotEmpty(array $values, array $choices);
Assertion::classExists(mixed $value);
Assertion::contains(mixed $string, string $needle);
Assertion::count(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count);
Assertion::count(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count);
Assertion::date(string $value, string $format);
Assertion::defined(mixed $constant);
Assertion::digit(mixed $value);
Expand Down Expand Up @@ -219,7 +219,7 @@ Assertion::ipv6(string $value, int $flag = null);
Assertion::isArray(mixed $value);
Assertion::isArrayAccessible(mixed $value);
Assertion::isCallable(mixed $value);
Assertion::isCountable(array|\Countable|\ResourceBundle|\SimpleXMLElement $value);
Assertion::isCountable(array|Countable|ResourceBundle|SimpleXMLElement $value);
Assertion::isInstanceOf(mixed $value, string $className);
Assertion::isJsonString(mixed $value);
Assertion::isObject(mixed $value);
Expand All @@ -232,11 +232,11 @@ Assertion::length(mixed $value, int $length);
Assertion::lessOrEqualThan(mixed $value, mixed $limit);
Assertion::lessThan(mixed $value, mixed $limit);
Assertion::max(mixed $value, mixed $maxValue);
Assertion::maxCount(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count);
Assertion::maxCount(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count);
Assertion::maxLength(mixed $value, int $maxLength);
Assertion::methodExists(string $value, mixed $object);
Assertion::min(mixed $value, mixed $minValue);
Assertion::minCount(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count);
Assertion::minCount(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count);
Assertion::minLength(mixed $value, int $minLength);
Assertion::noContent(mixed $value);
Assertion::notBlank(mixed $value);
Expand Down
74 changes: 38 additions & 36 deletions bin/MethodDocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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'];
}
}
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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')
);
Expand All @@ -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);
Expand All @@ -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)
);
Expand All @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion bin/generate_method_docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
33 changes: 16 additions & 17 deletions lib/Assert/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ abstract class Assert
* The invocation of this method starts an assertion chain
* that is happening on the passed value.
*
* @param mixed $value
* @param string $defaultMessage
* @param string $defaultPropertyPath
*
* @return AssertionChain
*
* @example
*
* Assert::that($value)->notEmpty()->integer();
* Assert::that($value)->nullOr()->string()->startsWith("Foo");
*
* The assertion chain can be stateful, that means be careful when you reuse
* it. You should never pass around the chain.
*
* @param mixed $value
* @param string $defaultMessage
* @param string $defaultPropertyPath
*
* @return \Assert\AssertionChain
*/
public static function that($value, $defaultMessage = null, $defaultPropertyPath = null)
public static function that($value, $defaultMessage = null, $defaultPropertyPath = null): AssertionChain
{
$assertionChain = new AssertionChain($value, $defaultMessage, $defaultPropertyPath);

Expand All @@ -55,43 +55,42 @@ public static function that($value, $defaultMessage = null, $defaultPropertyPath
/**
* Start validation on a set of values, returns {@link AssertionChain}.
*
* @param mixed $values
* @param mixed $values
* @param string $defaultMessage
* @param string $defaultPropertyPath
*
* @return \Assert\AssertionChain
* @return AssertionChain
*/
public static function thatAll($values, $defaultMessage = null, $defaultPropertyPath = null)
public static function thatAll($values, $defaultMessage = null, $defaultPropertyPath = null): AssertionChain
{
return static::that($values, $defaultMessage, $defaultPropertyPath)->all();
}

/**
* Start validation and allow NULL, returns {@link AssertionChain}.
*
* @param mixed $value
* @param mixed $value
* @param string $defaultMessage
* @param string $defaultPropertyPath
*
* @return \Assert\AssertionChain
* @return AssertionChain
*/
public static function thatNullOr($value, $defaultMessage = null, $defaultPropertyPath = null)
public static function thatNullOr($value, $defaultMessage = null, $defaultPropertyPath = null): AssertionChain
{
return static::that($value, $defaultMessage, $defaultPropertyPath)->nullOr();
}

/**
* Create a lazy assertion object.
*
* @return \Assert\LazyAssertion
* @return LazyAssertion
*/
public static function lazy()
public static function lazy(): LazyAssertion
{
$lazyAssertion = new LazyAssertion();

return $lazyAssertion
->setAssertClass(\get_called_class())
->setExceptionClass(static::$lazyAssertionExceptionClass)
;
->setExceptionClass(static::$lazyAssertionExceptionClass);
}
}
Loading

0 comments on commit dce360b

Please sign in to comment.