Skip to content

Commit

Permalink
Upgrade PHPStan from level 1 to level 7
Browse files Browse the repository at this point in the history
  • Loading branch information
tattali committed Dec 21, 2024
1 parent 08e9a71 commit 112e03f
Show file tree
Hide file tree
Showing 19 changed files with 247 additions and 413 deletions.
33 changes: 7 additions & 26 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,13 @@
return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@DoctrineAnnotation' => true,
'@PhpCsFixer:risky' => true,
'@PhpCsFixer' => true,
'@PHPUnit84Migration:risky' => true,
'@PSR1' => true,
'@PSR12:risky' => true,
'@PSR12' => true,
'@PSR2' => true,
'@Symfony:risky' => true,
'@PER:risky' => true,
'@PER' => true,
'@PHP82Migration:risky' => true,
'@PHP84Migration' => true,
'@PHPUnit100Migration:risky' => true,
'@Symfony' => true,
'array_indentation' => true,
'array_syntax' => [
'syntax' => 'short',
],
'doctrine_annotation_array_assignment' => [
'operator' => '=',
],
'doctrine_annotation_spaces' => [
'after_array_assignments_equals' => false,
'before_array_assignments_equals' => false,
],
'linebreak_after_opening_tag' => true,
'list_syntax' => [
'syntax' => 'short',
],
'@Symfony:risky' => true,
'no_extra_blank_lines' => [
'tokens' => [
'break',
Expand All @@ -48,7 +30,6 @@
'use',
],
],
'no_superfluous_phpdoc_tags' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_class_elements' => true,
Expand All @@ -68,7 +49,7 @@
'single_line_comment_style' => true,
'strict_comparison' => true,
'strict_param' => true,
'void_return' => false,
'php_unit_test_class_requires_covers' => false,
])
->setFinder($finder)
;
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
all: test

phpunit:
php vendor/bin/phpunit
XDEBUG_MODE=coverage | php vendor/bin/phpunit --display-phpunit-deprecations

lint:
php vendor/bin/php-cs-fixer fix --diff
php vendor/bin/phpcs --report=code
php vendor/bin/phpstan analyse
php vendor/bin/phpstan analyse src tests --level 7 --memory-limit=-1

test: lint phpunit
8 changes: 0 additions & 8 deletions codecov.yml

This file was deleted.

26 changes: 13 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@
}
],
"require": {
"php": ">=8.2.9",
"php": ">=8.2",
"mobiledetect/mobiledetectlib": "^4.8",
"symfony/dependency-injection": "^7.1",
"symfony/event-dispatcher": "^7.1",
"symfony/framework-bundle": "^7.1",
"symfony/yaml": "^7.1",
"twig/twig": "^3.14"
"symfony/dependency-injection": "^7.2",
"symfony/event-dispatcher": "^7.2",
"symfony/framework-bundle": "^7.2",
"symfony/yaml": "^7.2",
"twig/twig": "^3.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.65",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^1.12",
"phpstan/phpstan-nette": "^1.3",
"phpstan/phpstan-phpunit": "^1.4",
"phpunit/phpunit": "^11.4",
"squizlabs/php_codesniffer": "^3.10",
"symfony/dotenv": "^7.1",
"symfony/phpunit-bridge": "^7.1"
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"phpstan/phpstan-symfony": "^2.0",
"phpunit/phpunit": "^11.5",
"squizlabs/php_codesniffer": "^3.11",
"symfony/dotenv": "^7.2",
"symfony/phpunit-bridge": "^7.2"
},
"config": {
"sort-packages": true,
Expand Down
6 changes: 0 additions & 6 deletions phpstan.neon.dist

This file was deleted.

32 changes: 0 additions & 32 deletions phpunit.xml.dist

This file was deleted.

15 changes: 15 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sonar.projectKey=tattali_MobileDetectBundle
sonar.organization=tattali

sonar.php.coverage.reportPaths=build/coverage.xml

# This is the name and version displayed in the SonarCloud UI.
#sonar.projectName=CalendarBundle
#sonar.projectVersion=1.0


# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
sonar.sources=./src

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
47 changes: 21 additions & 26 deletions src/DataCollector/DeviceDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,20 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\VarDumper\Cloner\Data;

/**
* @author Jonas HAOUZI <[email protected]>
*/
class DeviceDataCollector extends DataCollector
{
/**
* @var DeviceView
* @param array<string, mixed> $redirectConfig
*/
protected $deviceView;

/**
* @var array
*/
protected $redirectConfig;

public function __construct(DeviceView $deviceView)
{
$this->deviceView = $deviceView;
public function __construct(
protected readonly DeviceView $deviceView,
protected readonly array $redirectConfig = [],
) {
}

/**
Expand All @@ -54,7 +49,7 @@ public function collect(
'label' => 'Full',
'link' => $this->generateSwitchLink(
$request,
DeviceView::VIEW_FULL
DeviceView::VIEW_FULL,
),
'isCurrent' => $this->deviceView->isFullView(),
'enabled' => $this->canUseView(DeviceView::VIEW_FULL, $request->getSchemeAndHttpHost()),
Expand All @@ -64,7 +59,7 @@ public function collect(
'label' => 'Tablet',
'link' => $this->generateSwitchLink(
$request,
DeviceView::VIEW_TABLET
DeviceView::VIEW_TABLET,
),
'isCurrent' => $this->deviceView->isTabletView(),
'enabled' => $this->canUseView(DeviceView::VIEW_TABLET, $request->getSchemeAndHttpHost()),
Expand All @@ -74,7 +69,7 @@ public function collect(
'label' => 'Mobile',
'link' => $this->generateSwitchLink(
$request,
DeviceView::VIEW_MOBILE
DeviceView::VIEW_MOBILE,
),
'isCurrent' => $this->deviceView->isMobileView(),
'enabled' => $this->canUseView(DeviceView::VIEW_MOBILE, $request->getSchemeAndHttpHost()),
Expand All @@ -87,22 +82,23 @@ public function getCurrentView(): string
return $this->data['currentView'];
}

/**
* @return array<string, mixed>
*/
public function getViews(): array
{
return $this->data['views'];
}

public function setRedirectConfig(array $redirectConfig): void
{
$this->redirectConfig = $redirectConfig;
}

public function getName(): string
{
return 'device.collector';
}

public function getData(): array
/**
* @return array<string, mixed>
*/
public function getData(): array|Data
{
return $this->data;
}
Expand All @@ -114,8 +110,7 @@ public function reset(): void

protected function canUseView(string $view, ?string $host): bool
{
if (!\is_array($this->redirectConfig)
|| !isset($this->redirectConfig[$view])
if (!isset($this->redirectConfig[$view])
|| !isset($this->redirectConfig[$view]['is_enabled'])
|| false === $this->redirectConfig[$view]['is_enabled']
) {
Expand All @@ -128,7 +123,7 @@ protected function canUseView(string $view, ?string $host): bool
&& \in_array($this->redirectConfig[$view]['action'], [RequestResponseListener::REDIRECT, RequestResponseListener::REDIRECT_WITHOUT_PATH], true)
) {
$parseHost = parse_url($this->redirectConfig[$view]['host']);
$redirectHost = $parseHost['scheme'].'://'.$parseHost['host'];
$redirectHost = ($parseHost['scheme'] ?? '').'://'.($parseHost['host'] ?? '');
if (!empty($parseHost['port'])) {
$redirectHost .= ':'.$parseHost['port'];
}
Expand All @@ -144,14 +139,14 @@ protected function canUseView(string $view, ?string $host): bool
private function generateSwitchLink(
Request $request,
string $view,
): ?string {
): string {
$requestSwitchView = $request->duplicate();
$requestSwitchView->query->set($this->deviceView->getSwitchParam(), $view);
$requestSwitchView->server->set(
'QUERY_STRING',
Request::normalizeQueryString(
http_build_query($requestSwitchView->query->all())
)
http_build_query($requestSwitchView->query->all()),
),
);

return $requestSwitchView->getUri();
Expand Down
3 changes: 0 additions & 3 deletions src/DependencyInjection/MobileDetectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

class MobileDetectExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
Expand Down
23 changes: 9 additions & 14 deletions src/EventListener/RequestResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,13 @@ class RequestResponseListener
public const TABLET = 'tablet';
public const FULL = 'full';

/**
* @var bool
*/
protected $needModifyResponse = false;
protected bool $needModifyResponse = false;

protected ?\Closure $modifyResponseClosure;

/**
* @var \Closure
* @param array<string, mixed> $redirectConf
*/
protected $modifyResponseClosure;

public function __construct(
protected readonly MobileDetect $mobileDetect,
protected readonly DeviceView $deviceView,
Expand All @@ -58,14 +55,14 @@ public function __construct(

public function handleRequest(RequestEvent $event): void
{
// only handle master request, do not handle sub request like esi includes
// Only handle main request, do not handle sub request like esi includes
// If the device view is "not the mobile view" (e.g. we're not in the request context)
if ((\defined('Symfony\Component\HttpKernel\HttpKernelInterface::MAIN_REQUEST') ? \constant('Symfony\Component\HttpKernel\HttpKernelInterface::MAIN_REQUEST') : \constant('Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST')) !== $event->getRequestType() || $this->deviceView->isNotMobileView()) {
if (!$event->isMainRequest() || $this->deviceView->isNotMobileView()) {
return;
}

$request = $event->getRequest();
$this->mobileDetect->setUserAgent($request->headers->get('user-agent'));
$this->mobileDetect->setUserAgent($request->headers->get('user-agent', ''));

// Sets the flag for the response handled by the GET switch param and the type of the view.
if ($this->deviceView->hasSwitchParam()) {
Expand Down Expand Up @@ -226,7 +223,7 @@ protected function getRedirectResponse(Request $request, string $view): ?Redirec
return $this->deviceView->getRedirectResponse(
$view,
$host,
$this->redirectConf[$view]['status_code']
$this->redirectConf[$view]['status_code'],
);
}

Expand All @@ -240,8 +237,6 @@ protected function getRedirectResponse(Request $request, string $view): ?Redirec
*/
protected function prepareResponseModification(string $view): void
{
$this->modifyResponseClosure = static function (DeviceView $deviceView, ResponseEvent $event) use ($view) {
return $deviceView->modifyResponse($view, $event->getResponse());
};
$this->modifyResponseClosure = static fn (DeviceView $deviceView, ResponseEvent $event) => $deviceView->modifyResponse($view, $event->getResponse());
}
}
Loading

0 comments on commit 112e03f

Please sign in to comment.