Skip to content

Enable type checker and fix warnings #26

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 8 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
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

#- name: Run type checker
# run: ./vendor/bin/psalm
- name: Run type checker
run: ./vendor/bin/psalm

- name: Run unit tests
run: ./vendor/bin/phpunit --testdox --no-coverage
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"require-dev": {
"guzzlehttp/psr7": "^2.4",
"mockery/mockery": "^1.4.1",
"phpunit/phpunit": "^8.0 || ^9.0 || ^10.0"
"phpunit/phpunit": "^8.0 || ^9.0 || ^10.0",
"vimeo/psalm": "^5.7.7"
},
"conflict": {
"phpunit/phpunit": "<8.0 || >= 11.0"
Expand Down
16 changes: 16 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
7 changes: 4 additions & 3 deletions src/Constraint/BodyMatchesConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BodyMatchesConstraint extends Constraint
{

/** @var Constraint */
private $constraint;
private Constraint $constraint;

public function __construct(Constraint $constraint)
{
Expand All @@ -23,17 +23,18 @@ public function __construct(Constraint $constraint)
*/
public function toString(): string
{
/** @psalm-suppress InternalMethod */
return 'message body matches ' . $this->constraint->toString();
}

protected function matches($other): bool
protected function matches(mixed $other): bool
{
if (!$other instanceof MessageInterface) {
return false;
}

$other->getBody()->rewind();
$body = $other->getBody()->getContents();
return $this->constraint->evaluate($body, '', true);
return (bool)$this->constraint->evaluate($body, '', true);
}
}
12 changes: 5 additions & 7 deletions src/Constraint/HasHeaderConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
class HasHeaderConstraint extends Constraint
{

/** @var string */
private $name;
private string $name;
private Constraint $constraint;

/** @var Constraint */
private $constraint;

public function __construct(string $name, $constraint = null)
public function __construct(string $name, Constraint|string|int $constraint = null)
{
if ($constraint === null) {
$constraint = Assert::logicalNot(Assert::isEmpty());
Expand All @@ -34,10 +31,11 @@ public function __construct(string $name, $constraint = null)
*/
public function toString(): string
{
/** @psalm-suppress InternalMethod */
return "has header '{$this->name}' that {$this->constraint->toString()}";
}

protected function matches($other): bool
protected function matches(mixed $other): bool
{
if (!$other instanceof MessageInterface) {
return false;
Expand Down
5 changes: 2 additions & 3 deletions src/Constraint/HasMethodConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
class HasMethodConstraint extends Constraint
{

/** @var string */
private $method;
private string $method;

public function __construct(string $method)
{
Expand All @@ -26,7 +25,7 @@ public function toString(): string
return "has request method {$this->method}";
}

protected function matches($other): bool
protected function matches(mixed $other): bool
{
if (!$other instanceof RequestInterface) {
return false;
Expand Down
9 changes: 4 additions & 5 deletions src/Constraint/HasQueryParameterConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

class HasQueryParameterConstraint extends Constraint
{
/** @var UrlEncodedMatches */
private $inner;
private UrlEncodedMatches $inner;

public function __construct($nameMatcher, $valueMatcher = null)
public function __construct(Constraint|string $nameMatcher, Constraint|string|null $valueMatcher = null)
{
$this->inner = new UrlEncodedMatches($nameMatcher, $valueMatcher);
}

protected function matches($other): bool
protected function matches(mixed $other): bool
{
if (is_string($other)) {
return $this->matchesString($other);
Expand Down Expand Up @@ -51,7 +50,7 @@ private function matchesString(string $other): bool

private function matchesQueryString(string $query): bool
{
return $this->inner->evaluate($query, "", true);
return (bool)$this->inner->evaluate($query, "", true);
}


Expand Down
7 changes: 5 additions & 2 deletions src/Constraint/HasQueryParametersConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
class HasQueryParametersConstraint extends Constraint
{
/** @var HasQueryParameterConstraint[] */
private $constraints = [];
private array $constraints = [];

/**
* @param array<string, HasQueryParameterConstraint> $constraints
*/
public function __construct(array $constraints)
{
foreach ($constraints as $key => $value) {
$this->constraints[] = new HasQueryParameterConstraint($key, $value);
}
}

public function matches($other): bool
public function matches(mixed $other): bool
{
foreach ($this->constraints as $constraint) {
if (!$constraint->evaluate($other, "", true)) {
Expand Down
12 changes: 6 additions & 6 deletions src/Constraint/HasStatusConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
class HasStatusConstraint extends Constraint
{

/** @var Constraint */
private $status;
private Constraint $status;

public function __construct($status)
public function __construct(mixed $status)
{
if (!$status instanceof Constraint) {
$status = Assert::equalTo($status);
Expand All @@ -28,19 +27,20 @@ public function __construct($status)
*/
public function toString(): string
{
/** @psalm-suppress InternalMethod */
return "response status {$this->status->toString()}";
}

protected function matches($other): bool
protected function matches(mixed $other): bool
{
if (!$other instanceof ResponseInterface) {
return false;
}

return $this->status->evaluate($other->getStatusCode(), '', true);
return (bool)$this->status->evaluate($other->getStatusCode(), '', true);
}

protected function additionalFailureDescription($other): string
protected function additionalFailureDescription(mixed $other): string
{
if ($other instanceof ResponseInterface) {
return 'Actual status is ' . $other->getStatusCode() . ' and the body contains: ' . $other->getBody();
Expand Down
5 changes: 2 additions & 3 deletions src/Constraint/HasUriConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
class HasUriConstraint extends Constraint
{

/** @var string */
private $uri;
private string $uri;

public function __construct(string $uri)
{
Expand All @@ -26,7 +25,7 @@ public function toString(): string
return "has request URI '{$this->uri}'";
}

protected function matches($other): bool
protected function matches(mixed $other): bool
{
if (!$other instanceof RequestInterface) {
return false;
Expand Down
8 changes: 6 additions & 2 deletions src/Constraint/IsAbsoluteUriConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ public function toString(): string
return "is valid URI";
}

protected function matches($other): bool
protected function matches(mixed $other): bool
{
if (!is_string($other)) {
return false;
}

$parts = parse_url($other);
if ($parts === false) {
return false;
Expand All @@ -26,6 +30,6 @@ protected function matches($other): bool
return false;
}

return $parts !== false;
return true;
}
}
14 changes: 10 additions & 4 deletions src/Constraint/UrlEncodedMatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

class UrlEncodedMatches extends Constraint
{
private $nameMatcher;
private $valueMatcher;
private Constraint $nameMatcher;
private Constraint $valueMatcher;

public function __construct($nameMatcher, $valueMatcher = null)
public function __construct(Constraint|string $nameMatcher, Constraint|string|null $valueMatcher = null)
{
if (!($nameMatcher instanceof Constraint)) {
$nameMatcher = new IsEqual($nameMatcher);
Expand All @@ -26,10 +26,15 @@ public function __construct($nameMatcher, $valueMatcher = null)
$this->valueMatcher = $valueMatcher;
}

protected function matches($other): bool
protected function matches(mixed $other): bool
{
if (!is_string($other)) {
return false;
}

parse_str($other, $parsedQuery);

/** @var array<string, string> $parsedQuery */
foreach ($parsedQuery as $key => $value) {
$nameMatches = $this->nameMatcher->evaluate($key, "", true);
$valueMatches = $this->valueMatcher->evaluate($value, "", true);
Expand All @@ -44,6 +49,7 @@ protected function matches($other): bool

public function toString(): string
{
/** @psalm-suppress InternalMethod */
return 'contains a name matching ' . $this->nameMatcher->toString() . ' and value matching ' . $this->valueMatcher->toString();
}

Expand Down
9 changes: 6 additions & 3 deletions src/Constraint/UrlEncodedMatchesMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
class UrlEncodedMatchesMany extends Constraint
{
/** @var UrlEncodedMatches[] */
private $constraints = [];
private array $constraints = [];

/**
* @param array<string, Constraint|string|null> $constraints
*/
public function __construct(array $constraints)
{
foreach ($constraints as $key => $value) {
$this->constraints[] = new UrlEncodedMatches($key, $value);
}
}

protected function matches($other): bool
protected function matches(mixed $other): bool
{
foreach ($this->constraints as $constraint) {
if (!$constraint->evaluate($other, "", true)) {
Expand All @@ -29,7 +32,7 @@ protected function matches($other): bool

public function toString(): string
{
return join(" and ", array_map(function(HasQueryParameterConstraint $c) {
return join(" and ", array_map(function(UrlEncodedMatches $c) {
return $c->toString();
}, $this->constraints));
}
Expand Down
23 changes: 18 additions & 5 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Helmich\JsonAssert\Constraint\JsonValueMatchesMany;
use Helmich\Psr7Assert\Constraint\BodyMatchesConstraint;
use Helmich\Psr7Assert\Constraint\HasHeaderConstraint;
use Helmich\Psr7Assert\Constraint\HasQueryParameterConstraint;
use Helmich\Psr7Assert\Constraint\HasUriConstraint;
use Helmich\Psr7Assert\Constraint\IsAbsoluteUriConstraint;
use Helmich\Psr7Assert\Psr7AssertionsClass;
Expand All @@ -15,26 +16,34 @@ function hasUri(string $uri): HasUriConstraint
return new HasUriConstraint($uri);
}

function hasHeader(string $name, $constraint = null): HasHeaderConstraint
function hasHeader(string $name, Constraint|string|int $constraint = null): HasHeaderConstraint
{
return new HasHeaderConstraint($name, $constraint);
}

/**
* @param array<string, Constraint|string> $constraints
* @return Constraint
*/
function hasHeaders(array $constraints): Constraint
{
return Psr7AssertionsClass::hasHeaders($constraints);
}

function hasStatus($status): Constraint
function hasStatus(Constraint|int $status): Constraint
{
return Psr7AssertionsClass::hasStatus($status);
}

function hasQueryParameter($name, $value = null): Constraint
function hasQueryParameter(Constraint|string $name, Constraint|string $value = null): Constraint
{
return Psr7AssertionsClass::hasQueryParameter($name, $value);
}

/**
* @param array<string, HasQueryParameterConstraint> $constraints
* @return Constraint
*/
function hasQueryParameters(array $constraints): Constraint
{
return Psr7AssertionsClass::hasQueryParameters($constraints);
Expand Down Expand Up @@ -93,12 +102,12 @@ function isDelete(): Constraint
return Psr7AssertionsClass::isDelete();
}

function bodyMatches($constraint): Constraint
function bodyMatches(Constraint $constraint): Constraint
{
return new BodyMatchesConstraint($constraint);
}

function bodyMatchesJson($constraints): Constraint
function bodyMatchesJson(array $constraints): Constraint
{
return Assert::logicalAnd(
hasContentType('application/json'),
Expand All @@ -111,6 +120,10 @@ function bodyMatchesJson($constraints): Constraint
);
}

/**
* @param array<string, Constraint|string|null> $constraints
* @return Constraint
*/
function bodyMatchesForm(array $constraints): Constraint
{
return Psr7AssertionsClass::bodyMatchesForm($constraints);
Expand Down
Loading