Skip to content

Commit

Permalink
Fix all errors returned by PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
jolelievre committed Nov 12, 2024
1 parent 9105d45 commit e711dfd
Show file tree
Hide file tree
Showing 41 changed files with 201 additions and 328 deletions.
2 changes: 0 additions & 2 deletions config/services/api/modules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ services:

PrestaShop\Module\Mbo\Module\SourceRetriever\AddonsUrlSourceRetriever:
autowire: true
arguments:
$modulePath: '%modules_dir%'
properties:
cacheDir: "%kernel.cache_dir%"

Expand Down
11 changes: 9 additions & 2 deletions ps_mbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public function __construct()
public function install(): bool
{
try {
$this->get(PrestaShop\PsAccountsInstaller\Installer\Installer::class)->install();
/** @var PrestaShop\PsAccountsInstaller\Installer\Installer|null $installer */
$installer = $this->get(PrestaShop\PsAccountsInstaller\Installer\Installer::class);
if ($installer) {
$installer->install();
}
} catch (Exception $e) {
ErrorHelper::reportError($e);
}
Expand Down Expand Up @@ -196,7 +200,9 @@ public function uninstall()
// Execute them first
foreach ($eventDispatcher->getListeners(ModuleManagementEvent::UNINSTALL) as $listener) {
if ($listener[0] instanceof ModuleManagementEventSubscriber) {
$legacyModule = $this->get(ModuleRepository::class)->getModule('ps_mbo');
/** @var ModuleRepository $moduleRepository */
$moduleRepository = $this->get(ModuleRepository::class);
$legacyModule = $moduleRepository->getModule('ps_mbo');
$listener[0]->{(string) $listener[1]}(new ModuleManagementEvent($legacyModule));
}
}
Expand Down Expand Up @@ -467,6 +473,7 @@ private function loadEnv(): void

private function isPsAccountEnabled(): bool
{
/** @var \PrestaShop\PsAccountsInstaller\Installer\Installer|null $accountsInstaller */
$accountsInstaller = $this->get(\PrestaShop\PsAccountsInstaller\Installer\Installer::class);

return null !== $accountsInstaller && $accountsInstaller->isModuleEnabled();
Expand Down
30 changes: 20 additions & 10 deletions src/Accounts/Provider/AccountsDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

use Db;
use Exception;
use PrestaShop\Module\PsAccounts\Repository\UserTokenRepository;
use Module;
use PrestaShop\PrestaShop\Adapter\ServiceLocator;
use PrestaShop\PsAccountsInstaller\Installer\Exception\ModuleNotInstalledException;
use PrestaShop\PsAccountsInstaller\Installer\Exception\ModuleVersionException;
Expand All @@ -49,16 +49,22 @@ public function getAccountsToken(): string
return '';
}

if (!class_exists(\PrestaShop\Module\PsAccounts\Repository\UserTokenRepository::class)) {
return '';
}

/** @var Module|null $psAccountsModule */
$psAccountsModule = ServiceLocator::get('ps_accounts');
if (null === $psAccountsModule || !method_exists($psAccountsModule, 'getService')) {
return '';
}

if (null === $psAccountsModule) {
/** @var \PrestaShop\Module\PsAccounts\Repository\UserTokenRepository|null $accountsUserTokenRepository */
$accountsUserTokenRepository = $psAccountsModule->getService(\PrestaShop\Module\PsAccounts\Repository\UserTokenRepository::class);

Check failure on line 63 in src/Accounts/Provider/AccountsDataProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (9.0.x)

PHPDoc tag @var for variable $accountsUserTokenRepository contains unknown class PrestaShop\Module\PsAccounts\Repository\UserTokenRepository.

Check failure on line 63 in src/Accounts/Provider/AccountsDataProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (9.0.x)

PHPDoc tag @var for variable $accountsUserTokenRepository contains unknown class PrestaShop\Module\PsAccounts\Repository\UserTokenRepository.
if (!$accountsUserTokenRepository) {
return '';
}

/**
* @var UserTokenRepository $accountsUserTokenRepository
*/
$accountsUserTokenRepository = $psAccountsModule->getService(UserTokenRepository::class);
try {
$token = $accountsUserTokenRepository->getOrRefreshToken();
} catch (Exception $e) {
Expand Down Expand Up @@ -115,8 +121,6 @@ private function isAccountLinked(): bool
}

/**
* @param string $serviceName
*
* @return mixed
*
* @throws ModuleNotInstalledException
Expand All @@ -126,8 +130,10 @@ public function getAccountsService()
{
if ($this->isPsAccountsInstalled()) {
if ($this->checkPsAccountsVersion()) {
return \Module::getInstanceByName(Installer::PS_ACCOUNTS_MODULE_NAME)
->getService(PsAccounts::PS_ACCOUNTS_SERVICE);
$module = \Module::getInstanceByName(Installer::PS_ACCOUNTS_MODULE_NAME);
if ($module && method_exists($module, 'getService')) {
return $module->getService(PsAccounts::PS_ACCOUNTS_SERVICE);
}
}
throw new ModuleVersionException('Module version expected : ' . $this->psAccountsVersion);
}
Expand All @@ -152,6 +158,10 @@ private function isPsAccountsInstalled()

private function checkPsAccountsVersion()
{
if (!class_exists('Ps_accounts')) {
return false;
}

$moduleName = Installer::PS_ACCOUNTS_MODULE_NAME;

$module = \Module::getInstanceByName($moduleName);
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Service/ModuleTransitionExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private function generateTokenizedModuleActionUrl($url): string
$composedUrl .= $baseUrl;

$queryParams = [];
if (is_array($components) && isset($components['query']) && is_string($components['query'])) {
if (is_array($components) && isset($components['query'])) {
parse_str($components['query'], $queryParams);
}

Expand Down
56 changes: 16 additions & 40 deletions src/Controller/Admin/AddonsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,16 @@
use PrestaShop\PrestaShop\Core\Module\ModuleRepository;
use PrestaShopBundle\Controller\Admin\PrestaShopAdminController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class AddonsController extends PrestaShopAdminController
{
/**
* @var RequestStack
*/
protected $requestStack;

/**
* @var ModuleManager
*/
private $moduleManager;

/**
* @var ModuleRepository
*/
private $moduleRepository;

public function __construct(
RequestStack $requestStack,
public function upgradeModuleAction(
Request $request,
ModuleManager $moduleManager,
ModuleRepository $moduleRepository
) {
parent::__construct();
$this->requestStack = $requestStack;
$this->moduleManager = $moduleManager;
$this->moduleRepository = $moduleRepository;
}

public function upgradeModuleAction(): JsonResponse
{
$request = $this->requestStack->getCurrentRequest();
ModuleRepository $moduleRepository,
): JsonResponse {
$moduleName = $request->request->get('moduleName');

if (null === $moduleName) {
Expand All @@ -70,30 +46,30 @@ public function upgradeModuleAction(): JsonResponse

try {
$upgradeResponse = [
'status' => $this->moduleManager->upgrade($moduleName),
'status' => $moduleManager->upgrade($moduleName),
'msg' => '',
'module_name' => $moduleName,
];

if ($upgradeResponse['status'] === true) {
$upgradeResponse['msg'] = $this->trans(
'Module %module% successfully upgraded.',
['%module%' => $moduleName],
'Modules.Mbo.Modulescatalog',
['%module%' => $moduleName]
);
$upgradeResponse['is_configurable'] = (bool) $this->moduleRepository
$upgradeResponse['is_configurable'] = (bool) $moduleRepository
->getModule($moduleName)
->attributes
->get('is_configurable');
} else {
$error = $this->moduleManager->getError($moduleName);
$error = $moduleManager->getError($moduleName);
$upgradeResponse['msg'] = $this->trans(
'Upgrade of module %module% failed. %error%',
'Modules.Mbo.Modulescatalog',
[
'%module%' => $moduleName,
'%error%' => $error,
]
],
'Modules.Mbo.Modulescatalog',
);
}
} catch (Exception $e) {
Expand All @@ -102,25 +78,25 @@ public function upgradeModuleAction(): JsonResponse
$upgradeResponse['status'] = true;
$upgradeResponse['msg'] = $this->trans(
'Module %module% is already up to date',
'Modules.Mbo.Modulescatalog',
[
'%module%' => $moduleName,
]
],
'Modules.Mbo.Modulescatalog',
);
} else {
try {
$this->moduleManager->disable($moduleName);
$moduleManager->disable($moduleName);
} catch (Exception $subE) {
ErrorHelper::reportError($subE);
}

$upgradeResponse['msg'] = $this->trans(
'Upgrade of module %module% failed. %error%',
'Modules.Mbo.Modulescatalog',
[
'%module%' => $moduleName,
'%error%' => $e->getMessage(),
]
],
'Modules.Mbo.Modulescatalog',
);
}
}
Expand Down
16 changes: 2 additions & 14 deletions src/Controller/Admin/ModuleCatalogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function indexAction(): Response
];

$cdcJsFile = getenv('MBO_CDC_URL');
if (false === $cdcJsFile || !is_string($cdcJsFile) || empty($cdcJsFile)) {
if (!is_string($cdcJsFile) || empty($cdcJsFile)) {
$extraParams['cdc_script_not_found'] = true;
$extraParams['cdc_error_url'] = $moduleUri . 'views/js/cdc-error.js';
} else {
Expand Down Expand Up @@ -191,14 +191,6 @@ public function cdcErrorAction(): Response

private function ensurePsAccountIsEnabled(): bool
{
if (version_compare(_PS_VERSION_, '9.0.0', '>=')) {
return false;
}

if (!$this->psAccountsInstaller) {
return false;
}

$accountsEnabled = $this->psAccountsInstaller->isModuleEnabled();
if ($accountsEnabled) {
return true;
Expand All @@ -207,12 +199,8 @@ private function ensurePsAccountIsEnabled(): bool
return $this->moduleManager->enable($this->psAccountsInstaller->getModuleName());
}

private function ensurePsEventbusEnabled()
private function ensurePsEventbusEnabled(): void
{
if (version_compare(_PS_VERSION_, '9.0.0', '>=')) {
return false;
}

if ($this->psEventbusInstaller->install()) {
$this->psEventbusInstaller->enable();
}
Expand Down
45 changes: 10 additions & 35 deletions src/Controller/Admin/ModuleRecommendedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use PrestaShopBundle\Controller\Admin\PrestaShopAdminController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;

Expand All @@ -35,42 +35,17 @@
*/
class ModuleRecommendedController extends PrestaShopAdminController
{
/**
* @var RequestStack
*/
protected $requestStack;

/**
* @var TabCollectionProvider
*/
private $tabCollectionProvider;

/**
* @var ContextBuilder
*/
private $contextBuilder;

/**
* @param RequestStack $requestStack
*/
public function __construct(
RequestStack $requestStack,
TabCollectionProvider $tabCollectionProvider,
ContextBuilder $contextBuilder
) {
$this->requestStack = $requestStack;
$this->tabCollectionProvider = $tabCollectionProvider;
$this->contextBuilder = $contextBuilder;
}

/**
* @return JsonResponse|RedirectResponse
*/
public function indexAction(): Response
{
public function indexAction(
Request $request,
TabCollectionProvider $tabCollectionProvider,
ContextBuilder $contextBuilder,
): Response {
$response = new JsonResponse();
try {
$tabClassName = $this->requestStack->getCurrentRequest()->get('tabClassName');
$tabClassName = $request->get('tabClassName');
if (null === $tabClassName) { // In case the recommended modules page is requested without giving tab context, we redirect to Modules catalog page
$routeParams = [];
$query = \Tools::getValue('bo_query');
Expand All @@ -80,10 +55,10 @@ public function indexAction(): Response

return $this->redirectToRoute('admin_mbo_catalog_module', $routeParams);
}
$tabCollection = $this->tabCollectionProvider->getTabCollection();
$tabCollection = $tabCollectionProvider->getTabCollection();
$tab = $tabCollection->getTab($tabClassName);
$context = $this->contextBuilder->getRecommendedModulesContext($tab);
$context['recommendation_format'] = $this->requestStack->getCurrentRequest()->get('recommendation_format');
$context = $contextBuilder->getRecommendedModulesContext($tab);
$context['recommendation_format'] = $request->get('recommendation_format');
$response->setData([
'content' => $this->renderView(
'@Modules/ps_mbo/views/templates/admin/controllers/module_catalog/recommended-modules.html.twig',
Expand Down
Loading

0 comments on commit e711dfd

Please sign in to comment.