Skip to content

Commit

Permalink
Add phpstan to project
Browse files Browse the repository at this point in the history
  • Loading branch information
muxx committed Jan 18, 2024
1 parent 031b009 commit eff6154
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ jobs:

- name: "Run code-style check"
run: composer phpcs

- name: "Run PHPStan check"
run: composer phpstan
8 changes: 4 additions & 4 deletions Cache/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ public function mSetNx($v)
return $result;
}

public function expire($key, $expire)
public function expire($key, $expire, $mode = null)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('expire');
}

$result = parent::expire($key, $expire);
$result = parent::expire($key, $expire, $mode);

if ($this->stopwatch) {
$e->stop();
Expand Down Expand Up @@ -192,13 +192,13 @@ public function sMembers($key)
return $result;
}

public function sAdd($tag, $id)
public function sAdd($tag, $id, ...$other_values)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('sAdd');
}

$result = parent::sAdd($tag, $id);
$result = parent::sAdd($tag, $id, ...$other_values);

if ($this->stopwatch) {
$e->stop();
Expand Down
4 changes: 3 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ public function getConfigTreeBuilder(): TreeBuilder
$treeBuilder = new TreeBuilder('intaro_pinba');
if (\method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
} elseif (\method_exists($treeBuilder, 'root')) {
// BC layer for symfony/config 4.1 and older
$rootNode = $treeBuilder->root('intaro_pinba');
} else {
throw new \RuntimeException('Unsupported symfony/config version');
}

$rootNode
Expand Down
4 changes: 2 additions & 2 deletions EventListener/ScriptNameConfigureListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function onRequest(KernelEvent $event): void
pinba_script_name_set($event->getRequest()->getRequestUri());
}

private static function bcEvent($event)
private static function bcEvent($event): bool
{
$eventClass = \Symfony\Component\HttpKernel\Event\GetResponseEvent::class;
$eventClass = 'Symfony\Component\HttpKernel\Event\GetResponseEvent';

return class_exists($eventClass) && !$event instanceof $eventClass;
}
Expand Down
4 changes: 1 addition & 3 deletions IntaroPinbaBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class IntaroPinbaBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
if(Kernel::MAJOR_VERSION >= 3) {
$container->addCompilerPass(new TwigPass());
}
$container->addCompilerPass(new TwigPass());
}
}
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ vendor: composer.json
phpcs: $(PHP_CONSOLE_DEPS)
@$(PHP) vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --using-cache=no -v

phpstan: $(PHP_CONSOLE_DEPS)
@$(PHP) vendor/bin/phpstan analyse

phpunit: $(PHP_CONSOLE_DEPS)
@$(PHP) vendor/bin/phpunit --color=always

check: phpcs phpunit
check: phpcs phpstan phpunit
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Install vendors:
make vendor
```

Run php-cs-fixer and phpunit:
Run php-cs-fixer, phpstan and phpunit:
```shell
make check
```
4 changes: 1 addition & 3 deletions Stopwatch/Stopwatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public function start(array $tags)
$tags = array_merge($this->initTags, $tags);
if (isset($tags['group']) && !isset($tags['category']) && false !== strpos($tags['group'], '::')) {
$v = explode('::', $tags['group']);
if (count($v) > 0) {
$tags['category'] = $v[0];
}
$tags['category'] = $v[0];
}
}

Expand Down
2 changes: 2 additions & 0 deletions Twig/TimedTwigEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TimedTwigEngine extends TwigEngine
*/
public function __construct(\Twig\Environment $environment, TemplateNameParserInterface $parser, FileLocatorInterface $locator, Stopwatch $stopwatch)
{
/* @phpstan-ignore-next-line */
parent::__construct($environment, $parser, $locator);

$this->stopwatch = $stopwatch;
Expand All @@ -53,6 +54,7 @@ public function render($name, array $parameters = [])
'twig_template' => (string) $name,
]);

/* @phpstan-ignore-next-line */
$ret = parent::render($name, $parameters);

$e->stop();
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"symfony/yaml": "^4.0|^5.0|^6.0"
},
"require-dev": {
"doctrine/dbal": "^2|^3|^4",
"nyholm/symfony-bundle-test": "^2.0",
"phpunit/phpunit": "^8.5|^9.5",
"symfony/phpunit-bridge": "^5.0|^6.0",
Expand All @@ -27,14 +28,16 @@
"symfony/routing": "^4.4|^5.0|^6.0",
"symfony/finder": "^4.4|^5.0|^6.0",
"symfony/filesystem": "^4.4|^5.0|^6.0",
"symfony/templating": "^4.4|^5.0|^6.0",
"symfony/translation-contracts": "^1.1|^2.0",
"symfony/var-exporter": "^4.4|^5.0|^6.0",
"symfony/var-dumper": "^4.4|^5.0|^6.0",
"psr/log": "~1.0|^2|^3",
"friendsofphp/php-cs-fixer": "3.4",
"twig/twig": "^v2.14 || ^3.0",
"symfony/twig-bundle": "^4.4|^5.0|^6.0",
"symfony/twig-bridge": "^4.4|^5.0|^6.0"
"symfony/twig-bridge": "^4.4|^5.0|^6.0",
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
Expand All @@ -51,7 +54,6 @@
"phpcs": [
"vendor/bin/php-cs-fixer fix --dry-run --config=.php-cs-fixer.dist.php --using-cache=no --show-progress=none -v"
],
"docker-clean": "docker image rm $(docker image ls -qf reference=pinba-bundle-test)",
"docker": "docker build . -t pinba-bundle-test && docker image prune -f >/dev/null && docker run --rm pinba-bundle-test"
"phpstan": "vendor/bin/phpstan analyse --no-progress"
}
}
26 changes: 26 additions & 0 deletions meta/phpstan/stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

if (!function_exists('pinba_get_info')) {
function pinba_get_info(): array
{
return [];
}
}

if (!function_exists('pinba_timer_start')) {
function pinba_timer_start(array $tags)
{
}
}

if (!function_exists('pinba_timer_stop')) {
function pinba_timer_stop($timer)
{
}
}

if (!function_exists('pinba_timer_add')) {
function pinba_timer_add(array $tags, int $value)
{
}
}
13 changes: 13 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
parameters:
level: 5
paths:
- .
excludePaths:
- vendor/
bootstrapFiles:
- meta/phpstan/stub.php
ignoreErrors:
# TODO remove after symfony >= 5.x
-
message: '#Access to an undefined static property Intaro\\PinbaBundle\\Tests\\BundleInitializationTest\:\:\$container#'
reportUnmatched: false
15 changes: 9 additions & 6 deletions tests/BundleInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Nyholm\BundleTest\TestKernel;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;

class BundleInitializationTest extends KernelTestCase
Expand All @@ -21,6 +22,7 @@ protected static function getKernelClass(): string

protected static function createKernel(array $options = []): KernelInterface
{
/** @var TestKernel $kernel */
$kernel = parent::createKernel($options);
$kernel->addTestBundle(IntaroPinbaBundle::class);
$kernel->addTestBundle(FixtureBundle::class);
Expand All @@ -33,9 +35,9 @@ protected static function createKernel(array $options = []): KernelInterface
public function testInitBundle(): void
{
self::bootKernel();
$symfony = self::getSymfonyVersion();
$symfonyVersion = self::getSymfonyVersion();

if (version_compare($symfony, '5.0.0') >= 0) {
if ($symfonyVersion >= 5) {
$container = self::getContainer();
} else {
$container = self::$container;
Expand All @@ -50,9 +52,9 @@ public function testInitBundle(): void
public function testTwigRenderStopwatch(): void
{
self::bootKernel();
$symfony = self::getSymfonyVersion();
$symfonyVersion = self::getSymfonyVersion();

if (version_compare($symfony, '5.0.0') >= 0) {
if ($symfonyVersion >= 5) {
$container = self::getContainer();
} else {
$container = self::$container;
Expand All @@ -62,10 +64,11 @@ public function testTwigRenderStopwatch(): void
$this->assertInstanceOf(TimedTwigEnvironment::class, $service);
}

private static function getSymfonyVersion()
private static function getSymfonyVersion(): int
{
/** @var Kernel $kernel */
$kernel = self::bootKernel();

return $kernel::VERSION;
return $kernel::MAJOR_VERSION;
}
}

0 comments on commit eff6154

Please sign in to comment.