Skip to content

Commit 2e34c73

Browse files
committed
psalm optimizations
1 parent 58d3ca2 commit 2e34c73

File tree

11 files changed

+45
-19
lines changed

11 files changed

+45
-19
lines changed

functions/container.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @param string|object $classContainer Класс контейнера.
1212
*
13-
* @return Container
13+
* @return ContainerInterface
1414
*
1515
* @since 21.03.2021 Класс (или объект) контейнера как параметр.
1616
*/
@@ -30,13 +30,13 @@ function container($classContainer = ServiceProvider::class)
3030
/**
3131
* Экземпляр манипулятора с делегированными контейнерами.
3232
*
33-
* @return ContainerInterface|null
33+
* @return null|object
3434
*
35-
* @throws Exception
35+
* @throws Exception Ошибки инициализации контейнера.
3636
*
3737
* @since 30.07.2021
3838
*/
39-
function delegatedContainer() : ?ContainerInterface
39+
function delegatedContainer() : ?object
4040
{
4141
return container()->get('delegated_container_manipulator');
4242
}

psalm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
xmlns="https://getpsalm.org/schema/config"
77
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
88
allowStringToStandInForClass="true"
9-
allowCoercionFromStringToClassConst="true"
109
findUnusedPsalmSuppress="true"
1110
skipChecksOnUnresolvableIncludes="true"
1211
>

src/Micro/AbstractStandaloneServiceProvider.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use Prokl\ServiceProvider\Framework\SymfonyCompilerPassBagLight;
77
use Prokl\ServiceProvider\ServiceProvider;
8+
use Psr\Container\ContainerInterface;
89
use Symfony\Component\Config\Loader\DelegatingLoader;
910
use Symfony\Component\DependencyInjection\ContainerBuilder;
1011

@@ -18,7 +19,7 @@
1819
class AbstractStandaloneServiceProvider extends ServiceProvider
1920
{
2021
/**
21-
* @var ContainerBuilder $containerBuilder Контейнер.
22+
* @var ContainerBuilder|ContainerInterface $containerBuilder Контейнер.
2223
*/
2324
protected static $containerBuilder;
2425

@@ -28,7 +29,11 @@ class AbstractStandaloneServiceProvider extends ServiceProvider
2829
protected $standartCompilerPasses;
2930

3031
/**
31-
* @inheritDoc
32+
* @param string $filename Имя файла.
33+
* @param string|null $pathBundlesConfig Путь к файлу с конфигурацией бандлов.
34+
*
35+
* @throws Exception
36+
* @psalm-suppress ConstructorSignatureMismatch
3237
*/
3338
public function __construct(
3439
string $filename,

src/Micro/ExampleMicroServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Prokl\ServiceProvider\Micro;
44

5+
use Psr\Container\ContainerInterface;
56
use Symfony\Component\DependencyInjection\ContainerBuilder;
67

78
/**
@@ -15,7 +16,7 @@
1516
class ExampleMicroServiceProvider extends AbstractStandaloneServiceProvider
1617
{
1718
/**
18-
* @var ContainerBuilder $containerBuilder Контейнер.
19+
* @var ContainerBuilder|ContainerInterface $containerBuilder Контейнер.
1920
*/
2021
protected static $containerBuilder;
2122

src/ServiceProvider.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
use Prokl\ServiceProvider\Services\AppKernel;
1414
use Prokl\ServiceProvider\Utils\ErrorScreen;
1515
use Prokl\ServiceProvider\Utils\Loaders\PhpLoaderSettingsBitrix;
16+
use Psr\Container\ContainerExceptionInterface;
1617
use Psr\Container\ContainerInterface;
1718
use Psr\Container\ContainerInterface as PsrContainerInterface;
19+
use Psr\Container\NotFoundExceptionInterface;
1820
use RuntimeException;
1921
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
2022
use Symfony\Component\Config\ConfigCache;
@@ -85,7 +87,7 @@ class ServiceProvider
8587
private const CONFIG_EXTS = '.{php,xml,yaml,yml}';
8688

8789
/**
88-
* @var ContainerBuilder $containerBuilder Контейнер.
90+
* @var ContainerBuilder|ContainerInterface $containerBuilder Контейнер.
8991
*/
9092
protected static $containerBuilder;
9193

@@ -197,6 +199,7 @@ public function __construct(
197199

198200
$this->filename = $filename;
199201

202+
/** @psalm-suppress RedundantConditionGivenDocblockType */
200203
if (static::$containerBuilder !== null) {
201204
return;
202205
}
@@ -231,10 +234,12 @@ public function get(string $id)
231234
/**
232235
* Контейнер.
233236
*
234-
* @return ContainerInterface
237+
* @return ContainerInterface|null
238+
* @throws Exception Ошибки инициализации контейнера.
235239
*/
236-
public function container() : ContainerInterface
240+
public function container() : ?ContainerInterface
237241
{
242+
/** @psalm-suppress RedundantConditionGivenDocblockType */
238243
return static::$containerBuilder ?: $this->initContainer($this->filename);
239244
}
240245

@@ -270,6 +275,7 @@ public function reboot() : void
270275
*/
271276
public function shutdown() : void
272277
{
278+
/** @psalm-suppress DocblockTypeContradiction */
273279
if (static::$containerBuilder === null) {
274280
return;
275281
}
@@ -340,15 +346,17 @@ private function boot() : void
340346
*
341347
* @param string $fileName Конфиг.
342348
*
343-
* @return ContainerBuilder|Container
349+
* @return ContainerBuilder|Container|ContainerInterface
344350
* @throws Exception Ошибки контейнера.
351+
* @throws ContainerExceptionInterface | NotFoundExceptionInterface Ошибки контейнера.
345352
*
346353
* @since 28.09.2020 Доработка.
347354
*/
348355
private function initContainer(string $fileName)
349356
{
350357
// Если в dev режиме, то не компилировать контейнер.
351358
if ((bool)$_ENV['DEBUG'] === true) {
359+
/** @psalm-suppress RedundantConditionGivenDocblockType */
352360
if (static::$containerBuilder !== null) {
353361
return static::$containerBuilder;
354362
}
@@ -465,6 +473,7 @@ private function dumpContainer(ConfigCache $cache, ContainerBuilder $container,
465473
$asFiles = $container->getParameter('container.dumper.inline_factories');
466474
}
467475

476+
/** @psalm-suppress ArgumentTypeCoercion */
468477
$dumper = new PhpDumper(static::$containerBuilder);
469478
if (class_exists(\ProxyManager\Configuration::class) && class_exists(ProxyDumper::class)) {
470479
$dumper->setProxyDumper(new ProxyDumper());
@@ -476,6 +485,7 @@ private function dumpContainer(ConfigCache $cache, ContainerBuilder $container,
476485
'file' => $cache->getPath(),
477486
'as_files' => $asFiles,
478487
'debug' => $this->debug,
488+
/** @psalm-suppress PossiblyUndefinedMethod */
479489
'build_time' => static::$containerBuilder->hasParameter('kernel.container_build_time')
480490
? static::$containerBuilder->getParameter('kernel.container_build_time') : time(),
481491
'preload_classes' => array_map('get_class', $this->bundles),
@@ -552,6 +562,7 @@ private function loadContainer(string $fileName)
552562
$this->setDefaultParamsContainer();
553563

554564
// Дополнить переменные приложения сведениями о зарегистрированных бандлах.
565+
/** @psalm-suppress PossiblyNullReference */
555566
static::$containerBuilder->get('kernel')->registerStandaloneBundles();
556567

557568
// Инициализация автономных бандлов.
@@ -644,7 +655,7 @@ private function initialize(string $fileName): ?ContainerBuilder
644655
$this->loadContainer($fileName);
645656

646657
$this->bundlesLoader->registerExtensions(static::$containerBuilder);
647-
658+
/** @psalm-suppress PossiblyUndefinedMethod */
648659
static::$containerBuilder->compile(true);
649660

650661
// Boot bundles.
@@ -671,6 +682,7 @@ private function initialize(string $fileName): ?ContainerBuilder
671682
*/
672683
private function setDefaultParamsContainer() : void
673684
{
685+
/** @psalm-suppress PossiblyUndefinedMethod */
674686
if (!static::$containerBuilder->hasDefinition('kernel')) {
675687
$this->registerKernel($this->kernelServiceClass);
676688
}
@@ -813,7 +825,6 @@ private function runPostLoadingPasses(): void
813825
/**
814826
* Отсортировать по приоритету.
815827
*
816-
* @psalm-suppress MissingClosureParamType
817828
* @psalm-suppress InvalidScalarArgument
818829
*/
819830
usort($this->postLoadingPassesBag, static function ($a, $b) : bool {

src/Services/AppKernel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ public function getLogDir()
110110
*/
111111
public function getProjectDir(): string
112112
{
113+
/** @psalm-suppress RedundantConditionGivenDocblockType */
114+
/** @psalm-suppress DocblockTypeContradiction */
113115
if ($this->projectDir === null) {
114116
$this->projectDir = Application::getDocumentRoot();
115117
}
@@ -201,6 +203,7 @@ public function setContainer(?ContainerInterface $container = null) : void
201203
*/
202204
public function getContainer()
203205
{
206+
/** @psalm-suppress DocblockTypeContradiction */
204207
if (static::$kernelContainer === null) {
205208
throw new LogicException('Cannot retrieve the container from a non-booted kernel.');
206209
}

src/Services/PSR/PSR7/PsrResponse.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public function withBody(StreamInterface $body)
159159
$newResponse = clone $this->response;
160160
$newResponse->setContent($body);
161161

162+
/** @psalm-suppress ImplicitToStringCast */
162163
return new static($newResponse, $this->httpVersion, $body);
163164
}
164165

src/Utils/DelegatedContainer/Manipulator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function get($id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFER
6666

6767
/**
6868
* @inheritdoc
69+
* @psalm-suppress MissingParamType
6970
*/
7071
public function has($id)
7172
{

src/Utils/ErrorScreen.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ public function __construct(
5656
/**
5757
* Показать экран смерти.
5858
*
59-
* @param string $message Сообщение об ошибке.
59+
* @param string $errorMessage Сообщение об ошибке.
6060
*
6161
* @return boolean
6262
*/
63-
public function die(string $message = '') : ?bool
63+
public function die(string $errorMessage = '') : ?bool
6464
{
6565
if (defined('PHPUNIT_COMPOSER_INSTALL') || defined('__PHPUNIT_PHAR__')) {
6666
throw new RuntimeException(
67-
$message
67+
$errorMessage
6868
);
6969
}
7070

71-
$content = $this->prepareErrorScreen($message);
71+
$content = $this->prepareErrorScreen($errorMessage);
7272

7373
$this->application->RestartBuffer();
7474
echo $content;

src/Utils/Loaders/PhpLoaderSettingsBitrix.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function load($resource, string $type = null)
3535
return $this->loadBitrixConfig('services', true);
3636
}, $this, ProtectedPhpFileLoader::class);
3737

38+
if (!$load) {
39+
return;
40+
}
41+
3842
try {
3943
$settings = $load($path, $this->env);
4044
if (is_array($settings)) {

0 commit comments

Comments
 (0)