Skip to content

Commit

Permalink
Merge pull request #251 from janvernieuwe/phpstan-next-level
Browse files Browse the repository at this point in the history
PHPStan level 4
  • Loading branch information
janvernieuwe authored Jul 12, 2019
2 parents 3839a84 + 5e3490d commit 7abfc60
Show file tree
Hide file tree
Showing 24 changed files with 123 additions and 57 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"phpstan/phpstan": "^0.11.8",
"phpunit/phpunit": "~6.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
"psr/http-message": "^1.0.1",
"robrichards/wse-php": "^2.0.2",
"robrichards/xmlseclibs": "^3.0",
"squizlabs/php_codesniffer": "~2.9",
Expand Down
4 changes: 3 additions & 1 deletion grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ parameters:
phpunit: ~
phplint: ~
phpstan:
level: 4
configuration: "phpstan.neon"
ignore_patterns:
- "spec/"
- "vendor/"
- "vendor/"
- "test"
8 changes: 6 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
parameters:
ignoreErrors:
- '#Call to an undefined static method SoapClient::__construct().#'
ignoreErrors:
# Soapclient reflection is quite broken
- '#Call to an undefined static method SoapClient::__construct().#'
# This is something we hope to fix in PHP 7.4
# https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters
- '#Call to an undefined method Phpro\\.*(Context|RuleSet)Interface.*$#'
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Phpro\SoapClient\Client;
use Phpro\SoapClient\Event\RequestEvent;
use Phpro\SoapClient\Event\Subscriber\ValidatorSubscriber;
use Phpro\SoapClient\Exception\RequestException;
use Phpro\SoapClient\Type\RequestInterface;
use PhpSpec\ObjectBehavior;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Phpro\SoapClient\Event\Subscriber\ValidatorSubscriber;

class ValidatorSubscriberSpec extends ObjectBehavior
{
Expand All @@ -34,11 +34,16 @@ function it_throws_exception_on_invalid_requests(
ValidatorInterface $validator,
Client $client,
RequestInterface $request,
ConstraintViolation $violation
ConstraintViolation $violation1,
ConstraintViolation $violation2
) {
$event = new RequestEvent($client->getWrappedObject(), 'method', $request->getWrappedObject());
$violation->__toString()->willReturn('error');
$validator->validate($request)->willReturn(new ConstraintViolationList([$violation->getWrappedObject()]));
$violation1->getMessage()->willReturn('error 1');
$violation2->getMessage()->willReturn('error 2');
$validator->validate($request)->willReturn(new ConstraintViolationList([
$violation1->getWrappedObject(),
$violation2->getWrappedObject(),
]));
$this->shouldThrow(RequestException::class)->duringOnClientRequest($event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
class InterfaceAssembler implements AssemblerInterface
{
/**
* @var
* @var string
*/
private $interfaceName;

/**
* InterfaceAssembler constructor.
*
* @param $interfaceName
* @param string $interfaceName
*/
public function __construct($interfaceName)
public function __construct(string $interfaceName)
{
$this->interfaceName = $interfaceName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class ResultProviderAssembler implements AssemblerInterface
/**
* ResultProviderAssembler constructor.
*
* @param null $wrapperClass
* @param string $wrapperClass
*/
public function __construct($wrapperClass = null)
public function __construct(string $wrapperClass = null)
{
$this->wrapperClass = ($wrapperClass !== null) ? ltrim($wrapperClass, '\\') : null;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Phpro/SoapClient/CodeGenerator/Assembler/UseAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ class UseAssembler implements AssemblerInterface
* @var string
*/
private $useName;

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

/**
* UseAssembler constructor.
* @param $useName
* @param $useAlias
* @param string $useName
* @param string $useAlias
*/
public function __construct(string $useName, $useAlias = null)
public function __construct(string $useName, string $useAlias = null)
{
$this->useName = $useName;
$this->useAlias = $useAlias;
Expand Down
2 changes: 1 addition & 1 deletion src/Phpro/SoapClient/CodeGenerator/ClientGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(RuleSetInterface $ruleSet)
*/
public function generate(FileGenerator $file, $client): string
{
$class = $file->getClass() ?: new ClassGenerator();
$class = $file->getClass() ?? new ClassGenerator();
$class->setNamespaceName($client->getNamespace());
$class->setName($client->getName());
$methods = $client->getMethodMap();
Expand Down
2 changes: 1 addition & 1 deletion src/Phpro/SoapClient/CodeGenerator/Model/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(string $name, string $type)
$this->type = Normalizer::normalizeClassnameInFQN($type);
}

public static function fromMetadata(string $parameterNamespace, MetadataParameter $parameter)
public static function fromMetadata(string $parameterNamespace, MetadataParameter $parameter): Parameter
{
$type = $parameter->getType()->getBaseTypeOrFallbackToName();

Expand Down
1 change: 1 addition & 0 deletions src/Phpro/SoapClient/CodeGenerator/Rules/TypeMapRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function appliesToContext(ContextInterface $context): bool
}

// It's possible to define a null rule, which means that no code will be generated.
/** @var RuleInterface|null $rule */
$rule = $this->typeMap[$type->getName()];
if ($rule === null) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Phpro/SoapClient/CodeGenerator/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(RuleSetInterface $ruleSet)
*/
public function generate(FileGenerator $file, $type): string
{
$class = $file->getClass() ?: new ClassGenerator();
$class = $file->getClass() ?? new ClassGenerator();
$class->setNamespaceName($type->getNamespace());
$class->setName($type->getName());

Expand Down
2 changes: 1 addition & 1 deletion src/Phpro/SoapClient/CodeGenerator/Util/TypeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TypeChecker
private static $internalPhpTypes = ['void', 'int', 'float', 'string', 'bool', 'array', 'callable', 'iterable'];

/**
* @param $type
* @param string $type
*
* @return bool
*/
Expand Down
19 changes: 12 additions & 7 deletions src/Phpro/SoapClient/Console/Command/GenerateClassmapCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
/**
* Generates one type class
*
* @param FileGenerator $file
* @param FileGenerator $file
* @param ClassMapGenerator $generator
* @param TypeMap $typeMap
* @param $path
* @param TypeMap $typeMap
* @param string $path
*/
protected function generateClassmap(FileGenerator $file, ClassMapGenerator $generator, TypeMap $typeMap, $path)
{
protected function generateClassmap(
FileGenerator $file,
ClassMapGenerator $generator,
TypeMap $typeMap,
string $path
) {
$code = $generator->generate($file, $typeMap);
$this->filesystem->putFileContents($path, $code);
}
Expand All @@ -117,10 +121,11 @@ protected function generateClassmap(FileGenerator $file, ClassMapGenerator $gene
*
* @param ClassMapGenerator $generator
* @param TypeMap $typeMap
* @param $path
* @param string $path
*
* @return bool
*/
protected function handleClassmap(ClassMapGenerator $generator, TypeMap $typeMap, $path): bool
protected function handleClassmap(ClassMapGenerator $generator, TypeMap $typeMap, string $path): bool
{
// Handle existing class:
if ($this->filesystem->fileExists($path)) {
Expand Down
21 changes: 11 additions & 10 deletions src/Phpro/SoapClient/Console/Command/GenerateClientCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Phpro\SoapClient\Console\Command;

use Phpro\SoapClient\CodeGenerator\ClientGenerator;
use Phpro\SoapClient\CodeGenerator\GeneratorInterface;
use Phpro\SoapClient\CodeGenerator\Model\Client;
use Phpro\SoapClient\CodeGenerator\Model\ClientMethodMap;
use Phpro\SoapClient\CodeGenerator\TypeGenerator;
Expand Down Expand Up @@ -101,11 +102,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
* Generates one type class
*
* @param FileGenerator $file
* @param ClientGenerator|TypeGenerator $generator
* @param GeneratorInterface $generator
* @param Client $client
* @param string $path
*/
protected function generateClient(FileGenerator $file, ClientGenerator $generator, Client $client, $path)
protected function generateClient(FileGenerator $file, GeneratorInterface $generator, Client $client, string $path)
{
$code = $generator->generate($file, $client);
$this->filesystem->putFileContents($path, $code);
Expand All @@ -117,12 +118,12 @@ protected function generateClient(FileGenerator $file, ClientGenerator $generato
* If patching the old class does not work: ask for an overwrite
* Create a class from an empty file
*
* @param ClientGenerator|TypeGenerator $generator
* @param ClientGenerator $generator
* @param Client $client
* @param string $path
* @return bool
*/
protected function handleClient(ClientGenerator $generator, Client $client, $path)
protected function handleClient(ClientGenerator $generator, Client $client, string $path): bool
{
// Handle existing class:
if ($this->filesystem->fileExists($path)) {
Expand Down Expand Up @@ -154,14 +155,14 @@ protected function handleClient(ClientGenerator $generator, Client $client, $pat
/**
* An existing file was found. Try to patch or ask if it can be overwritten.
*
* @param TypeGenerator $generator
* @param GeneratorInterface $generator
* @param Client $client
* @param string $path
* @return bool
*/
protected function handleExistingFile(TypeGenerator $generator, Client $client, $path)
protected function handleExistingFile(GeneratorInterface $generator, Client $client, $path): bool
{
$this->output->write(sprintf('Type %s exists. Trying to patch ...', $client->getName()));
$this->output->write(sprintf('Class %s exists. Trying to patch ...', $client->getName()));
$patched = $this->patchExistingFile($generator, $client, $path);

if ($patched) {
Expand All @@ -178,13 +179,13 @@ protected function handleExistingFile(TypeGenerator $generator, Client $client,
/**
* This method tries to patch an existing type class.
*
* @param TypeGenerator $generator
* @param GeneratorInterface $generator
* @param Client $client
* @param string $path
* @return bool
* @internal param Type $type
*/
protected function patchExistingFile(TypeGenerator $generator, Client $client, $path)
protected function patchExistingFile(GeneratorInterface $generator, Client $client, $path): bool
{
try {
$this->filesystem->createBackup($path);
Expand All @@ -203,7 +204,7 @@ protected function patchExistingFile(TypeGenerator $generator, Client $client, $
/**
* @return bool
*/
protected function askForOverwrite()
protected function askForOverwrite(): bool
{
$overwriteByDefault = $this->input->getOption('overwrite');
$question = new ConfirmationQuestion('Do you want to overwrite it?', $overwriteByDefault);
Expand Down
2 changes: 1 addition & 1 deletion src/Phpro/SoapClient/Console/Helper/ConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getName():string
/**
* Attempts to load the configuration file, returns it on success
* @param InputInterface $input
* @return Config
* @return ConfigInterface
*/
public function load(InputInterface $input): ConfigInterface
{
Expand Down
29 changes: 21 additions & 8 deletions src/Phpro/SoapClient/Event/Subscriber/ValidatorSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Phpro\SoapClient\Events;
use Phpro\SoapClient\Exception\RequestException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class ValidatorSubscriber implements EventSubscriberInterface
Expand All @@ -25,6 +27,16 @@ public function __construct(ValidatorInterface $validator)
$this->validator = $validator;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
Events::REQUEST => 'onClientRequest',
];
}

/**
* @param RequestEvent $event
*
Expand All @@ -35,17 +47,18 @@ public function onClientRequest(RequestEvent $event)
$errors = $this->validator->validate($event->getRequest());

if (count($errors)) {
throw new RequestException((string) $errors);
throw new RequestException(self::toString($errors));
}
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
private static function toString(ConstraintViolationListInterface $errors): string
{
return [
Events::REQUEST => 'onClientRequest',
];
$strErrors = [];
/** @var ConstraintViolationInterface $error */
foreach ($errors as $error) {
$strErrors[] = $error->getMessage();
}

return implode(PHP_EOL, $strErrors);
}
}
18 changes: 18 additions & 0 deletions src/Phpro/SoapClient/Exception/MiddlewareException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Phpro\SoapClient\Exception;

use Http\Client\Exception;
use Throwable;

class MiddlewareException extends RuntimeException
{
public static function fromHttPlugException(Exception $exception): self
{
if ($exception instanceof Throwable) {
return new self($exception->getMessage(), $exception->getCode(), $exception);
}

return new self('Something went wrong: '.get_class($exception));
}
}
3 changes: 2 additions & 1 deletion src/Phpro/SoapClient/Middleware/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Http\Client\Exception;
use Http\Promise\Promise;
use Phpro\SoapClient\Exception\MiddlewareException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -44,6 +45,6 @@ public function afterResponse(ResponseInterface $response): ResponseInterface

public function onError(Exception $exception)
{
throw $exception;
throw MiddlewareException::fromHttPlugException($exception);
}
}
Loading

0 comments on commit 7abfc60

Please sign in to comment.