Skip to content

Commit 598c43b

Browse files
committed
Merge branch 'master' into v3
* master: CS Fixes Add appropriate error suppressions for PHP 7.0 More type hint fixes found by static analysis Code styling Type hints bugfix add typehint add composer require ext add throw phpdoc
2 parents 5547e7d + 410caeb commit 598c43b

24 files changed

+844
-574
lines changed

.php_cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,31 @@ obtain it through the world-wide-web, please send an email
1515
to kontakt@beberlei.de so I can send you a copy immediately.
1616
TXT;
1717

18-
$rules = array(
18+
$rules = [
1919
'@PSR2' => true,
2020
'@Symfony' => true,
21-
'concat_space' => false,
22-
'native_function_invocation' => true,
21+
'cast_spaces' => [
22+
'space' => 'none',
23+
],
24+
'concat_space' => [
25+
'spacing' => 'none',
26+
],
27+
'native_function_invocation' => [
28+
'scope' => 'namespaced',
29+
],
2330
'psr4' => true,
24-
'phpdoc_align' => true,
25-
'array_syntax' => ['syntax' => 'short'],
26-
'header_comment' => array(
31+
'phpdoc_align' => [
32+
'align' => 'left',
33+
],
34+
'array_syntax' => [
35+
'syntax' => 'short',
36+
],
37+
'header_comment' => [
2738
'header' => $header,
2839
'commentType' => PhpCsFixer\Fixer\Comment\HeaderCommentFixer::HEADER_PHPDOC,
29-
),
30-
);
40+
],
41+
'yoda_style' => false,
42+
];
3143

3244
$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;
3345

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ Assertion::choice(mixed $value, array $choices);
191191
Assertion::choicesNotEmpty(array $values, array $choices);
192192
Assertion::classExists(mixed $value);
193193
Assertion::contains(mixed $string, string $needle);
194-
Assertion::count(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count);
194+
Assertion::count(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count);
195195
Assertion::date(string $value, string $format);
196196
Assertion::defined(mixed $constant);
197197
Assertion::digit(mixed $value);
@@ -219,7 +219,7 @@ Assertion::ipv6(string $value, int $flag = null);
219219
Assertion::isArray(mixed $value);
220220
Assertion::isArrayAccessible(mixed $value);
221221
Assertion::isCallable(mixed $value);
222-
Assertion::isCountable(array|\Countable|\ResourceBundle|\SimpleXMLElement $value);
222+
Assertion::isCountable(array|Countable|ResourceBundle|SimpleXMLElement $value);
223223
Assertion::isInstanceOf(mixed $value, string $className);
224224
Assertion::isJsonString(mixed $value);
225225
Assertion::isObject(mixed $value);
@@ -232,11 +232,11 @@ Assertion::length(mixed $value, int $length);
232232
Assertion::lessOrEqualThan(mixed $value, mixed $limit);
233233
Assertion::lessThan(mixed $value, mixed $limit);
234234
Assertion::max(mixed $value, mixed $maxValue);
235-
Assertion::maxCount(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count);
235+
Assertion::maxCount(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count);
236236
Assertion::maxLength(mixed $value, int $maxLength);
237237
Assertion::methodExists(string $value, mixed $object);
238238
Assertion::min(mixed $value, mixed $minValue);
239-
Assertion::minCount(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count);
239+
Assertion::minCount(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count);
240240
Assertion::minLength(mixed $value, int $minLength);
241241
Assertion::noContent(mixed $value);
242242
Assertion::notBlank(mixed $value);

bin/MethodDocGenerator.php

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
* to kontakt@beberlei.de so I can send you a copy immediately.
1313
*/
1414

15+
use Assert\Assertion;
16+
use Assert\AssertionFailedException;
17+
1518
/**
1619
* Class MethodDocGenerator.
1720
*/
@@ -30,50 +33,50 @@ public function generateChainDocs()
3033

3134
/**
3235
* @param ReflectionMethod[] $methods
33-
* @param string $format
34-
* @param callable|false $skipParameterTest
35-
* @param string $prefix
36+
* @param string $format
37+
* @param callable|false $skipParameterTest
38+
* @param string $prefix
3639
*
3740
* @return array
3841
*
39-
* @throws \Assert\AssertionFailedException
42+
* @throws AssertionFailedException
4043
*/
4144
private function generateMethodDocs($methods, $format, $skipParameterTest, $prefix = '')
4245
{
4346
$lines = [];
44-
\asort($methods);
47+
asort($methods);
4548
foreach ($methods as $method) {
4649
$doc = $method->getDocComment();
47-
list(, $descriptionLine) = \explode("\n", $doc);
48-
$shortDescription = \trim(\substr($descriptionLine, 7), '.');
49-
$methodName = $prefix.($prefix ? \ucfirst($method->getName()) : $method->getName());
50+
list(, $descriptionLine) = explode("\n", $doc);
51+
$shortDescription = trim(substr($descriptionLine, 7), '.');
52+
$methodName = $prefix.($prefix ? ucfirst($method->getName()) : $method->getName());
5053

51-
if (\preg_match('`\* This is an alias of {@see (?P<aliasOf>[^\s\}]++)`sim', $doc, $aliasMatch)) {
52-
$shortDescription .= \sprintf('. This is an alias of Assertion::%s()', \trim($aliasMatch['aliasOf'], '(){}'));
54+
if (preg_match('`\* This is an alias of {@see (?P<aliasOf>[^\s\}]++)`sim', $doc, $aliasMatch)) {
55+
$shortDescription .= sprintf('. This is an alias of Assertion::%s()', trim($aliasMatch['aliasOf'], '(){}'));
5356
}
5457

5558
$parameters = [];
5659

5760
foreach ($method->getParameters() as $parameterIndex => $methodParameter) {
5861
if (
59-
(\is_bool($skipParameterTest) && $skipParameterTest) ||
60-
(\is_callable($skipParameterTest) && $skipParameterTest($methodParameter))
62+
(is_bool($skipParameterTest) && $skipParameterTest) ||
63+
(is_callable($skipParameterTest) && $skipParameterTest($methodParameter))
6164
) {
6265
continue;
6366
}
6467

6568
$parameter = '$'.$methodParameter->getName();
6669

67-
$type = \version_compare(PHP_VERSION, '7.0.0') >= 0 ? $methodParameter->getType() : null;
70+
$type = version_compare(PHP_VERSION, '7.0.0') >= 0 ? $methodParameter->getType() : null;
6871

69-
if (\is_null($type)) {
70-
\preg_match(\sprintf('`\* @param (?P<type>[^ ]++) +\%s\b`sim', $parameter), $doc, $matches);
72+
if (is_null($type)) {
73+
preg_match(sprintf('`\* @param (?P<type>[^ ]++) +\%s\b`sim', $parameter), $doc, $matches);
7174
if (isset($matches['type'])) {
7275
$type = (
7376
$methodParameter->isOptional() &&
7477
null == $methodParameter->getDefaultValue()
7578
)
76-
? \str_replace(['|null', 'null|'], '', $matches['type'])
79+
? str_replace(['|null', 'null|'], '', $matches['type'])
7780
: $matches['type'];
7881
}
7982
}
@@ -82,21 +85,21 @@ private function generateMethodDocs($methods, $format, $skipParameterTest, $pref
8285
$type .= '|null';
8386
}
8487

85-
\Assert\Assertion::notEmpty($type, \sprintf('No type defined for %s in %s', $parameter, $methodName));
86-
$parameter = \sprintf('%s %s', $type, $parameter);
88+
Assertion::notEmpty($type, sprintf('No type defined for %s in %s', $parameter, $methodName));
89+
$parameter = sprintf('%s %s', $type, $parameter);
8790

8891
if ($methodParameter->isOptional()) {
8992
if (null === $methodParameter->getDefaultValue()) {
9093
$parameter .= ' = null';
9194
} else {
92-
$parameter .= \sprintf(' = \'%s\'', $methodParameter->getDefaultValue());
95+
$parameter .= sprintf(' = \'%s\'', $methodParameter->getDefaultValue());
9396
}
9497
}
9598

9699
$parameters[] = $parameter;
97100
}
98101

99-
$lines[] = \sprintf($format, $methodName, \implode(', ', $parameters), $shortDescription);
102+
$lines[] = sprintf($format, $methodName, implode(', ', $parameters), $shortDescription);
100103
}
101104

102105
return $lines;
@@ -109,14 +112,14 @@ private function gatherAssertions()
109112
{
110113
$reflClass = new ReflectionClass('Assert\Assertion');
111114

112-
return \array_filter(
115+
return array_filter(
113116
$reflClass->getMethods(ReflectionMethod::IS_STATIC),
114117
function (ReflectionMethod $reflMethod) {
115118
if ($reflMethod->isProtected()) {
116119
return false;
117120
}
118121

119-
if (\in_array($reflMethod->getName(), ['__callStatic', 'createException', 'stringify'])) {
122+
if (in_array($reflMethod->getName(), ['__callStatic', 'createException', 'stringify'])) {
120123
return false;
121124
}
122125

@@ -126,33 +129,33 @@ function (ReflectionMethod $reflMethod) {
126129
}
127130

128131
/**
129-
* @param string $phpFile
132+
* @param string $phpFile
130133
* @param string[] $lines
131-
* @param string $fileType
134+
* @param string $fileType
132135
*/
133136
private function generateFile($phpFile, $lines, $fileType)
134137
{
135-
$phpFile = \realpath($phpFile);
136-
$fileContent = \file_get_contents($phpFile);
138+
$phpFile = realpath($phpFile);
139+
$fileContent = file_get_contents($phpFile);
137140

138141
switch ($fileType) {
139142
case 'class':
140-
$fileContent = \preg_replace(
143+
$fileContent = preg_replace(
141144
'`\* @method.*? \*/\nclass `sim',
142-
\sprintf("%s\n */\nclass ", \trim(\implode("\n", $lines))),
145+
sprintf("%s\n */\nclass ", trim(implode("\n", $lines))),
143146
$fileContent
144147
);
145148
break;
146149
case 'readme':
147-
$fileContent = \preg_replace(
150+
$fileContent = preg_replace(
148151
'/```php\n<\?php\nuse Assert\\\Assertion;\n\nAssertion::.*?```/sim',
149-
\sprintf("```php\n<?php\nuse Assert\\Assertion;\n\n%s\n\n```", \implode("\n", $lines)),
152+
sprintf("```php\n<?php\nuse Assert\\Assertion;\n\n%s\n\n```", implode("\n", $lines)),
150153
$fileContent
151154
);
152155
break;
153156
}
154157

155-
$writtenBytes = \file_put_contents($phpFile, $fileContent);
158+
$writtenBytes = file_put_contents($phpFile, $fileContent);
156159

157160
if (false !== $writtenBytes) {
158161
echo 'Generated '.$phpFile.'.'.PHP_EOL;
@@ -166,7 +169,7 @@ public function generateAssertionDocs()
166169
return false;
167170
};
168171

169-
$docs = \array_merge(
172+
$docs = array_merge(
170173
$this->generateMethodDocs($this->gatherAssertions(), ' * @method static bool %s(%s) %s for all values.', $skipParameterTest, 'all'),
171174
$this->generateMethodDocs($this->gatherAssertions(), ' * @method static bool %s(%s) %s or that the value is null.', $skipParameterTest, 'nullOr')
172175
);
@@ -178,7 +181,7 @@ public function generateReadMe()
178181
{
179182
$mdFile = __DIR__.'/../README.md';
180183
$skipParameterTest = function (ReflectionParameter $parameter) {
181-
return \in_array($parameter->getName(), ['message', 'propertyPath', 'encoding']);
184+
return in_array($parameter->getName(), ['message', 'propertyPath', 'encoding']);
182185
};
183186

184187
$docs = $this->generateMethodDocs($this->gatherAssertions(), 'Assertion::%s(%s);', $skipParameterTest);
@@ -193,7 +196,7 @@ public function generateLazyAssertionDocs()
193196
return 0 === $parameter->getPosition();
194197
};
195198

196-
$docs = \array_merge(
199+
$docs = array_merge(
197200
$this->generateMethodDocs($this->gatherAssertions(), ' * @method $this %s(%s) %s.', $skipParameterTest),
198201
$this->generateMethodDocs($this->gatherAssertionChainSwitches(), ' * @method $this %s(%s) %s.', false)
199202
);
@@ -208,14 +211,14 @@ private function gatherAssertionChainSwitches()
208211
{
209212
$reflClass = new ReflectionClass('Assert\AssertionChain');
210213

211-
return \array_filter(
214+
return array_filter(
212215
$reflClass->getMethods(ReflectionMethod::IS_PUBLIC),
213216
function (ReflectionMethod $reflMethod) {
214217
if (!$reflMethod->isPublic()) {
215218
return false;
216219
}
217220

218-
if (\in_array($reflMethod->getName(), ['__construct', '__call', 'setAssertionClassName'])) {
221+
if (in_array($reflMethod->getName(), ['__construct', '__call', 'setAssertionClassName'])) {
219222
return false;
220223
}
221224

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
"sort-packages": true
2424
},
2525
"require": {
26-
"php": "^7"
26+
"php": "^7",
27+
"ext-intl": "*",
28+
"ext-simplexml": "*",
29+
"ext-mbstring": "*",
30+
"ext-ctype": "*",
31+
"ext-json": "*"
2732
},
2833
"require-dev": {
2934
"friendsofphp/php-cs-fixer": "*",

lib/Assert/Assert.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ abstract class Assert
3131
* The invocation of this method starts an assertion chain
3232
* that is happening on the passed value.
3333
*
34+
* @param mixed $value
35+
* @param string|callable|null $defaultMessage
36+
* @param string|null $defaultPropertyPath
37+
*
38+
* @return AssertionChain
39+
*
3440
* @example
3541
*
3642
* Assert::that($value)->notEmpty()->integer();
3743
* Assert::that($value)->nullOr()->string()->startsWith("Foo");
3844
*
3945
* The assertion chain can be stateful, that means be careful when you reuse
4046
* it. You should never pass around the chain.
41-
*
42-
* @param mixed $value
43-
* @param string $defaultMessage
44-
* @param string $defaultPropertyPath
45-
*
46-
* @return \Assert\AssertionChain
4747
*/
48-
public static function that($value, $defaultMessage = null, $defaultPropertyPath = null)
48+
public static function that($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
4949
{
5050
$assertionChain = new AssertionChain($value, $defaultMessage, $defaultPropertyPath);
5151

@@ -55,43 +55,42 @@ public static function that($value, $defaultMessage = null, $defaultPropertyPath
5555
/**
5656
* Start validation on a set of values, returns {@link AssertionChain}.
5757
*
58-
* @param mixed $values
59-
* @param string $defaultMessage
60-
* @param string $defaultPropertyPath
58+
* @param mixed $values
59+
* @param string|callable|null $defaultMessage
60+
* @param string|null $defaultPropertyPath
6161
*
62-
* @return \Assert\AssertionChain
62+
* @return AssertionChain
6363
*/
64-
public static function thatAll($values, $defaultMessage = null, $defaultPropertyPath = null)
64+
public static function thatAll($values, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
6565
{
6666
return static::that($values, $defaultMessage, $defaultPropertyPath)->all();
6767
}
6868

6969
/**
7070
* Start validation and allow NULL, returns {@link AssertionChain}.
7171
*
72-
* @param mixed $value
73-
* @param string $defaultMessage
74-
* @param string $defaultPropertyPath
72+
* @param mixed $value
73+
* @param string|callable|null $defaultMessage
74+
* @param string|null $defaultPropertyPath
7575
*
76-
* @return \Assert\AssertionChain
76+
* @return AssertionChain
7777
*/
78-
public static function thatNullOr($value, $defaultMessage = null, $defaultPropertyPath = null)
78+
public static function thatNullOr($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
7979
{
8080
return static::that($value, $defaultMessage, $defaultPropertyPath)->nullOr();
8181
}
8282

8383
/**
8484
* Create a lazy assertion object.
8585
*
86-
* @return \Assert\LazyAssertion
86+
* @return LazyAssertion
8787
*/
88-
public static function lazy()
88+
public static function lazy(): LazyAssertion
8989
{
9090
$lazyAssertion = new LazyAssertion();
9191

9292
return $lazyAssertion
9393
->setAssertClass(\get_called_class())
94-
->setExceptionClass(static::$lazyAssertionExceptionClass)
95-
;
94+
->setExceptionClass(static::$lazyAssertionExceptionClass);
9695
}
9796
}

0 commit comments

Comments
 (0)