Skip to content

Add-return-types #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bin/validate-json
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,10 @@ function getUrlFromPath($path)
/**
* Take a HTTP header value and split it up into parts.
*
* @param $headerValue
* @return array Key "_value" contains the main value, all others
* as given in the header value
*/
function parseHeaderValue($headerValue)
function parseHeaderValue($headerValue): array
{
if (strpos($headerValue, ';') === false) {
return ['_value' => $headerValue];
Expand Down
1,729 changes: 1,537 additions & 192 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
parameters:
level: 8
level: max
paths:
- ./src/
ignoreErrors: []
- ./tests/
phpVersion: 70200

includes:
Expand Down
5 changes: 1 addition & 4 deletions src/JsonSchema/ConstraintError.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ class ConstraintError extends Enum
public const TYPE = 'type';
public const UNIQUE_ITEMS = 'uniqueItems';

/**
* @return string
*/
public function getMessage()
public function getMessage(): string
{
$name = $this->getValue();
static $messages = [
Expand Down
1 change: 0 additions & 1 deletion src/JsonSchema/Constraints/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ public function setConstraintClass(string $name, string $class): Factory
* @throws InvalidArgumentException if is not possible create the constraint instance
*
* @return ConstraintInterface&BaseConstraint
* @phpstan-return ConstraintInterface&BaseConstraint
*/
public function createInstanceFor($constraintName)
{
Expand Down
18 changes: 9 additions & 9 deletions src/JsonSchema/Constraints/FormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =
}
}

protected function validateDateTime($datetime, $format)
protected function validateDateTime($datetime, $format): bool
{
$dt = \DateTime::createFromFormat($format, (string) $datetime);

Expand All @@ -184,7 +184,7 @@ protected function validateDateTime($datetime, $format)
return false;
}

protected function validateRegex($regex)
protected function validateRegex($regex): bool
{
if (!is_string($regex)) {
return true;
Expand All @@ -193,7 +193,7 @@ protected function validateRegex($regex)
return false !== @preg_match(self::jsonPatternToPhpRegex($regex), '');
}

protected function validateColor($color)
protected function validateColor($color): bool
{
if (!is_string($color)) {
return true;
Expand All @@ -205,30 +205,30 @@ protected function validateColor($color)
return true;
}

return preg_match('/^#([a-f0-9]{3}|[a-f0-9]{6})$/i', $color);
return 1 === preg_match('/^#([a-f0-9]{3}|[a-f0-9]{6})$/i', $color);
}

protected function validateStyle($style)
protected function validateStyle($style): bool
{
$properties = explode(';', rtrim($style, ';'));
$invalidEntries = preg_grep('/^\s*[-a-z]+\s*:\s*.+$/i', $properties, PREG_GREP_INVERT);

return empty($invalidEntries);
}

protected function validatePhone($phone)
protected function validatePhone($phone): bool
{
return preg_match('/^\+?(\(\d{3}\)|\d{3}) \d{3} \d{4}$/', $phone);
return 1 === preg_match('/^\+?(\(\d{3}\)|\d{3}) \d{3} \d{4}$/', $phone);
}

protected function validateHostname($host)
protected function validateHostname($host): bool
{
if (!is_string($host)) {
return true;
}

$hostnameRegex = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/i';

return preg_match($hostnameRegex, $host);
return 1 === preg_match($hostnameRegex, $host);
}
}
2 changes: 1 addition & 1 deletion src/JsonSchema/Constraints/NumberConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =
$this->checkFormat($element, $schema, $path, $i);
}

private function fmod($number1, $number2)
private function fmod($number1, $number2): float
{
$modulus = ($number1 - round($number1 / $number2) * $number2);
$precision = 0.0000000001;
Expand Down
11 changes: 7 additions & 4 deletions src/JsonSchema/Constraints/ObjectConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $pro
$this->validateElement($element, $matches, $schema, $path, $properties, $additionalProp);
}

public function validatePatternProperties($element, ?JsonPointer $path, $patternProperties)
/**
* @return list<string>
*/
public function validatePatternProperties($element, ?JsonPointer $path, $patternProperties): array
{
$matches = [];
foreach ($patternProperties as $pregex => $schema) {
Expand Down Expand Up @@ -87,7 +90,7 @@ public function validatePatternProperties($element, ?JsonPointer $path, $pattern
* @param mixed $additionalProp Additional properties
*/
public function validateElement($element, $matches, $schema = null, ?JsonPointer $path = null,
$properties = null, $additionalProp = null)
$properties = null, $additionalProp = null): void
{
$this->validateMinMaxConstraint($element, $schema, $path);

Expand Down Expand Up @@ -131,7 +134,7 @@ public function validateElement($element, $matches, $schema = null, ?JsonPointer
* @param \stdClass $properties Property definitions
* @param JsonPointer|null $path Path?
*/
public function validateProperties(&$element, $properties = null, ?JsonPointer $path = null)
public function validateProperties(&$element, $properties = null, ?JsonPointer $path = null): void
{
$undefinedConstraint = $this->factory->createInstanceFor('undefined');

Expand Down Expand Up @@ -173,7 +176,7 @@ protected function &getProperty(&$element, $property, $fallback = null)
* @param \stdClass $objectDefinition ObjectConstraint definition
* @param JsonPointer|null $path Path to test?
*/
protected function validateMinMaxConstraint($element, $objectDefinition, ?JsonPointer $path = null)
protected function validateMinMaxConstraint($element, $objectDefinition, ?JsonPointer $path = null): void
{
// Verify minimum number of properties
if (isset($objectDefinition->minProperties) && !is_object($objectDefinition->minProperties)) {
Expand Down
10 changes: 8 additions & 2 deletions src/JsonSchema/Constraints/StringConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use JsonSchema\ConstraintError;
use JsonSchema\Entity\JsonPointer;
use JsonSchema\Exception\RuntimeException;

/**
* The StringConstraint Constraints, validates an string against a given schema
Expand Down Expand Up @@ -51,10 +52,15 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =
$this->checkFormat($element, $schema, $path, $i);
}

private function strlen($string)
private function strlen($string): int
{
if (extension_loaded('mbstring')) {
return mb_strlen($string, mb_detect_encoding($string));
$result = mb_strlen($string, mb_detect_encoding($string));
if ($result === false) {
throw new RuntimeException('mb_strlen() failed.');
}

return $result;
}

// mbstring is present on all test platforms, so strlen() can be ignored for coverage
Expand Down
17 changes: 9 additions & 8 deletions src/JsonSchema/Constraints/TypeCheck/LooseTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@

class LooseTypeCheck implements TypeCheckInterface
{
public static function isObject($value)
public static function isObject($value): bool
{
return
is_object($value) ||
(is_array($value) && (count($value) == 0 || self::isAssociativeArray($value)));
}

public static function isArray($value)
public static function isArray($value): bool
{
return
is_array($value) &&
(count($value) == 0 || !self::isAssociativeArray($value));
}

/**
* @return mixed
*/
public static function propertyGet($value, $property)
{
if (is_object($value)) {
Expand All @@ -29,7 +32,7 @@ public static function propertyGet($value, $property)
return $value[$property];
}

public static function propertySet(&$value, $property, $data)
public static function propertySet(&$value, $property, $data): void
{
if (is_object($value)) {
$value->{$property} = $data;
Expand All @@ -38,7 +41,7 @@ public static function propertySet(&$value, $property, $data)
}
}

public static function propertyExists($value, $property)
public static function propertyExists($value, $property): bool
{
if (is_object($value)) {
return property_exists($value, $property);
Expand All @@ -47,7 +50,7 @@ public static function propertyExists($value, $property)
return array_key_exists($property, $value);
}

public static function propertyCount($value)
public static function propertyCount($value): int
{
if (is_object($value)) {
return count(get_object_vars($value));
Expand All @@ -60,10 +63,8 @@ public static function propertyCount($value)
* Check if the provided array is associative or not
*
* @param array $arr
*
* @return bool
*/
private static function isAssociativeArray($arr)
private static function isAssociativeArray($arr): bool
{
return array_keys($arr) !== range(0, count($arr) - 1);
}
Expand Down
10 changes: 5 additions & 5 deletions src/JsonSchema/Constraints/TypeCheck/StrictTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

class StrictTypeCheck implements TypeCheckInterface
{
public static function isObject($value)
public static function isObject($value): bool
{
return is_object($value);
}

public static function isArray($value)
public static function isArray($value): bool
{
return is_array($value);
}
Expand All @@ -21,17 +21,17 @@ public static function propertyGet($value, $property)
return $value->{$property};
}

public static function propertySet(&$value, $property, $data)
public static function propertySet(&$value, $property, $data): void
{
$value->{$property} = $data;
}

public static function propertyExists($value, $property)
public static function propertyExists($value, $property): bool
{
return property_exists($value, $property);
}

public static function propertyCount($value)
public static function propertyCount($value): int
{
if (!is_object($value)) {
return 0;
Expand Down
13 changes: 8 additions & 5 deletions src/JsonSchema/Constraints/TypeCheck/TypeCheckInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

interface TypeCheckInterface
{
public static function isObject($value);
public static function isObject($value): bool;

public static function isArray($value);
public static function isArray($value): bool;

/**
* @return mixed
*/
public static function propertyGet($value, $property);

public static function propertySet(&$value, $property, $data);
public static function propertySet(&$value, $property, $data): void;

public static function propertyExists($value, $property);
public static function propertyExists($value, $property): bool;

public static function propertyCount($value);
public static function propertyCount($value): int;
}
12 changes: 4 additions & 8 deletions src/JsonSchema/Constraints/TypeConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function check(&$value = null, $schema = null, ?JsonPointer $path = null,
* @param ?JsonPointer $path
* @param bool $coerce
*/
protected function validateTypesArray(&$value, array $type, &$validTypesWording, &$isValid, $path, $coerce = false)
protected function validateTypesArray(&$value, array $type, &$validTypesWording, &$isValid, $path, $coerce = false): void
{
foreach ($type as $tp) {
// already valid, so no need to waste cycles looping over everything
Expand Down Expand Up @@ -128,10 +128,8 @@ protected function validateTypesArray(&$value, array $type, &$validTypesWording,
* @param array $elements The elements to implode
* @param string $delimiter The delimiter to use
* @param bool $listEnd The last delimiter to use (defaults to $delimiter)
*
* @return string
*/
protected function implodeWith(array $elements, $delimiter = ', ', $listEnd = false)
protected function implodeWith(array $elements, $delimiter = ', ', $listEnd = false): string
{
if ($listEnd === false || !isset($elements[1])) {
return implode($delimiter, $elements);
Expand All @@ -151,7 +149,7 @@ protected function implodeWith(array $elements, $delimiter = ', ', $listEnd = fa
*
* @throws StandardUnexpectedValueException
*/
protected function validateTypeNameWording($type)
protected function validateTypeNameWording($type): void
{
if (!array_key_exists($type, self::$wording)) {
throw new StandardUnexpectedValueException(
Expand All @@ -170,10 +168,8 @@ protected function validateTypeNameWording($type)
* @param string $type TypeConstraint to check against
*
* @throws InvalidArgumentException
*
* @return bool
*/
protected function validateType(&$value, $type, $coerce = false)
protected function validateType(&$value, $type, $coerce = false): bool
{
//mostly the case for inline schema
if (!$type) {
Expand Down
Loading
Loading