Skip to content
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

add PHPStan #2

Open
wants to merge 2 commits into
base: add-gh-actions
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
2 changes: 0 additions & 2 deletions .bootstrap.atoum.php

This file was deleted.

86 changes: 43 additions & 43 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,49 @@ jobs:
- name: Run PHP-CS-Fixer fix
run: php-cs-fixer fix --dry-run --diff --ansi

# phpstan:
# name: PHPStan
# runs-on: ubuntu-latest
# timeout-minutes: 20
# env:
# APP_DEBUG: '1' # https://github.com/phpstan/phpstan-symfony/issues/37
# steps:
# - name: Checkout
# uses: actions/checkout@v2
# - name: Setup PHP
# uses: shivammathur/setup-php@v2
# with:
# php-version: ${{ matrix.php }}
# tools: pecl, composer
# extensions: intl, bcmath, curl, openssl, mbstring
# coverage: none
# ini-values: memory_limit=-1
# - name: Get composer cache directory
# id: composercache
# run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
# - name: Cache dependencies
# uses: actions/cache@v2
# with:
# path: ${{ steps.composercache.outputs.dir }}
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
# restore-keys: ${{ runner.os }}-composer-
# - name: Update project dependencies
# run: composer update --no-interaction --no-progress --ansi
# run: bin/simple-phpunit --version
# - name: Cache PHPStan results
# uses: actions/cache@v2
# with:
# path: /tmp/phpstan
# key: phpstan-php${{ matrix.php }}-${{ github.sha }}
# restore-keys: |
# phpstan-php${{ matrix.php }}-
# phpstan-
# continue-on-error: true
# - name: Clear test app cache
# run: |
# tests/Fixtures/app/console cache:clear --ansi
# - name: Run PHPStan analysis
# run: ./bin/phpstan analyse --no-interaction --no-progress --no-interaction --ansi
phpstan:
name: PHPStan
runs-on: ubuntu-latest
strategy:
matrix:
php:
- '8.1'
fail-fast: false
env:
APP_DEBUG: '1' # https://github.com/phpstan/phpstan-symfony/issues/37
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: intl, bcmath, curl, openssl, mbstring
ini-values: memory_limit=-1
tools: pecl, composer, phpstan
coverage: none
- name: Get composer cache directory
id: composercache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Update project dependencies
run: composer update --no-interaction --no-progress --ansi
- name: Cache PHPStan results
uses: actions/cache@v2
with:
path: /tmp/phpstan
key: phpstan-php${{ matrix.php }}-${{ github.sha }}
restore-keys: |
phpstan-php${{ matrix.php }}-
phpstan-
continue-on-error: true
- name: Run PHPStan analysis
run: phpstan analyse --no-interaction --no-progress --no-interaction --ansi

atoum:
name: Atoum (PHP ${{ matrix.php }})
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
"behat/mink-selenium2-driver": "^1.6",
"atoum/atoum": "^4.0",
"fabpot/goutte": "^3.2",
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"atoum/stubs": "^2.6"
},

"autoload": {
"psr-4": {
"Behatch\\": "src/"
"Behatch\\": "src/",
"Behatch\\Tests\\Units\\": "tests/units/"
}
},

Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 3
paths:
- src
- tests
scanDirectories:
- vendor/atoum/stubs/classes
12 changes: 3 additions & 9 deletions src/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,7 @@ public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema):
*/
public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema): void
{
$this->not(function () use ($schema) {
return $this->theJsonShouldBeValidAccordingToThisSchema($schema);
}, 'Expected to receive invalid json, got valid one');
$this->not([$this, 'theJsonShouldBeValidAccordingToThisSchema'], 'Expected to receive invalid json, got valid one');
}

/**
Expand All @@ -341,9 +339,7 @@ public function theJsonShouldBeInvalidAccordingToTheSchema($filename): void
{
$this->checkSchemaFile($filename);

$this->not(function () use ($filename) {
return $this->theJsonShouldBeValidAccordingToTheSchema($filename);
}, 'The schema was valid');
$this->not([$this, 'theJsonShouldBeValidAccordingToTheSchema'], 'The schema was valid');
}

/**
Expand Down Expand Up @@ -404,9 +400,7 @@ public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $sche
*/
public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName): void
{
$this->not(function () use ($dumpPath, $schemaName) {
return $this->theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName);
}, 'JSON Schema matches but it should not');
$this->not([$this, 'theJsonShouldBeValidAccordingToTheSwaggerSchema'], 'JSON Schema matches but it should not');
}

protected function getJson()
Expand Down
2 changes: 1 addition & 1 deletion src/Context/RestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function iSendARequestTo($method, $url, PyStringNode $body = null, $files
$this->locatePath($url),
[],
$files,
null !== $body ? $body->getRaw() : null
$body?->getRaw()
);
}

Expand Down
10 changes: 6 additions & 4 deletions src/Context/XmlContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ public function theXmlElementShouldBeEqualTo($element, $text): void
*/
public function theXmlElementShouldNotBeEqualTo($element, $text): void
{
$this->not(function () use ($element, $text): void {
$this->theXmlElementShouldBeEqualTo($element, $text);
}, "The element '$element' value is not '$text'");
$this->not([$this, 'theXmlElementShouldBeEqualTo'], "The element '$element' value is not '$text'");
}

/**
Expand All @@ -104,7 +102,11 @@ public function theXmlAttributeShouldExist($attribute, $element)
{
$elements = $this->theXmlElementShouldExist("{$element}[@{$attribute}]");

$actual = $elements->item(0)->getAttribute($attribute);
if (!($element = $elements->item(0)) instanceof \DOMElement) {
throw new \Exception('Unable to retrieve the node attribute');
}

$actual = $element->getAttribute($attribute);

if (empty($actual)) {
throw new \Exception("The attribute value is '$actual'");
Expand Down
28 changes: 17 additions & 11 deletions src/HttpCall/HttpCallResultPoolResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class HttpCallResultPoolResolver implements ArgumentResolver
{
private $dependencies;
private array $dependencies;

public function __construct(/* ... */)
{
Expand All @@ -21,17 +21,23 @@ public function __construct(/* ... */)

public function resolveArguments(\ReflectionClass $classReflection, array $arguments)
{
$constructor = $classReflection->getConstructor();
if (null !== $constructor) {
$parameters = $constructor->getParameters();
foreach ($parameters as $parameter) {
$class = \PHP_VERSION_ID < 80000 ? $parameter->getClass() : ($parameter->getType() && !$parameter->getType()->isBuiltin()
? new \ReflectionClass($parameter->getType()->getName())
: null
);
if (null !== $class && isset($this->dependencies[$class->name])) {
$arguments[$parameter->name] = $this->dependencies[$class->name];
if (null !== ($constructor = $classReflection->getConstructor())) {
foreach ($constructor->getParameters() as $parameter) {
if (!($type = $parameter->getType()) instanceof \ReflectionNamedType) {
continue;
}

if ($type->isBuiltin()) {
continue;
}

$class = new \ReflectionClass($type->getName());

if (!isset($this->dependencies[$class->name])) {
continue;
}

$arguments[$parameter->name] = $this->dependencies[$class->name];
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/HttpCall/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@

namespace Behatch\HttpCall;

use Behat\Mink\Element\DocumentElement;
use Behat\Mink\Mink;

/**
* @method DocumentElement send(string $method, string $url, array $parameters = [], array $files = [], string $content = null, array $headers = [])
* @method string getMethod()
* @method string getUri()
* @method array getServer()
* @method array getParameters()
* @method array getHttpHeaders()
* @method string getHttpHeader(string $name)
* @method void setHttpHeader(string $name, string $value)
* @method string getHttpRawHeader(string $name)
* @method string getContent()
*/
class Request
{
/**
Expand Down
39 changes: 17 additions & 22 deletions tests/units/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,26 @@

class JsonContext extends \atoum
{
/**
* @var HttpCallResultPool
*/
private $httpCallResultPool;
private HttpCallResultPool $httpCallResultPool;

public function beforeTestMethod($methodName): void
{
$this->mockGenerator->orphanize('__construct');
$httpCallResult = $this->newMockInstance(HttpCallResult::class);
$httpCallResult->getMockController()->getValue = json_encode([
'a string node' => 'some string',
'another string node' => 'some other string',
'a null node' => null,
'a true node' => true,
'a false node' => false,
'a number node' => 3,
'an array node' => [
'one',
'two',
'three',
],
]);

$this->httpCallResultPool = $this->newMockInstance(HttpCallResultPool::class);
$this->httpCallResultPool->getMockController()->getResult = $httpCallResult;
$this->httpCallResultPool = new HttpCallResultPool();
$this->httpCallResultPool->store(
new HttpCallResult(json_encode([
'a string node' => 'some string',
'another string node' => 'some other string',
'a null node' => null,
'a true node' => true,
'a false node' => false,
'a number node' => 3,
'an array node' => [
'one',
'two',
'three',
],
], \JSON_THROW_ON_ERROR))
);
}

public function testTheJsonNodeShouldBeEqualTo(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/units/Json/JsonInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function test_validate(): void
{
$json = new \Behatch\Json\Json('{ "foo": { "bar": "foobar" } }');
$inspector = $this->newTestedInstance('php');
$schema = new \mock\Behatch\Json\JsonSchema('{}');
$schema = new \Behatch\Json\JsonSchema('{}');

$result = $inspector->validate($json, $schema);

Expand Down