Skip to content

Commit

Permalink
Merge pull request #2159 from spryker/feature/sdk-2432-add-container-…
Browse files Browse the repository at this point in the history
…set-fucntion-check

SDK-2432. Added container-set-function.md
  • Loading branch information
andriitserkovnyi authored Sep 14, 2023
2 parents 5ec1e91 + 73ac4b5 commit c008a3c
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 24 deletions.
2 changes: 2 additions & 0 deletions _data/sidebars/scos_dev_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4792,6 +4792,8 @@ entries:
url: /docs/scos/dev/guidelines/keeping-a-project-upgradable/upgradability-guidelines/dead-code-checker.html
- title: Additional logic in dependency provider
url: /docs/scos/dev/guidelines/keeping-a-project-upgradable/upgradability-guidelines/additional-logic-in-dependency-provider.html
- title: Container set function
url: /docs/scos/dev/guidelines/keeping-a-project-upgradable/upgradability-guidelines/container-set-function.html
- title: Minimum allowed shop version
url: /docs/scos/dev/guidelines/keeping-a-project-upgradable/upgradability-guidelines/minimum-allowed-shop-version.html
- title: Multidimensional array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FormDependencyProvider extends SprykerFormDependencyProvider
2. `isDevelopment` function call

The usage of `isDevelopment` checks is allowed in order to register the plugins that are needed in development mode only (e.g. profiling, debug, etc.).

```php
namespace Pyz\Zed\Console;

Expand All @@ -63,7 +63,7 @@ class ConsoleDependencyProvider extends SprykerConsoleDependencyProvider
protected function getConsoleCommands(Container $container): array
{
$commands = [];

if ($this->getConfig()->isDevelopmentConsoleCommandsEnabled()) {
$commands[] = new CodeTestConsole();
}
Expand Down Expand Up @@ -113,9 +113,7 @@ class FormDependencyProvider extends SprykerFormDependencyProvider
}
```
### Resolving the error
To resolve the issue:
## Resole the error
1. Try to avoid the usage of conditions in the dependency providers.
2. Use only the supported expressions in the `if` construct.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
title: Container set function
description: Reference information for evaluator tools.
template: howto-guide-template
---

The *Container set function* check checks how plugins are registered in the dependency provider on the project level.

## Problem description

In the dependency provider, you can add plugins to `\Spryker\Client\Kernel\Container` by using the `set` method. To keep the plugins simple, do not return arrays of plugins inside the callback function. When you need to register a list of plugins, create a dedicated method, return an array of plugins, and call this method inside the callable function.

## Example of an evaluator error message

```bash
==============================
CONTAINER SET FUNCTION CHECKER
==============================

Message: The callback function inside `container->set()` should not return an array directly but instead call another method. Please review your code and make the necessary changes.
Target: {PATH_TO_PROJECT}/Pyz/Zed/Checkout/CheckoutDependencyProvider.php:{LINE_NUMBER}
```

## Example of code that causes an evaluator error

The method `addProductSalePageWidgetPlugins` in `ExampleDependencyProvider` contains a callable function that returns an array of plugins.

```php
namespace Pyz\Zed\ContainerSetFunctionChecker;
use Spryker\Zed\WebProfiler\Communication\Plugin\Form\WebProfilerFormPlugin;
class ExampleDependencyProvider
{
/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
protected function addProductSalePageWidgetPlugins($container): Container
{
$container->set(static::PYZ_PLUGIN_PRODUCT_SALE_PAGE_WIDGETS, function () {
return [
new WebProfilerFormPlugin(),
];
});
return $container;
}
}
```
## Resolve the error
1. Create a dedicated method for registering plugins.
2. Call the method in the callback function.
```php
namespace Pyz\Zed\ContainerSetFunctionChecker;
use Spryker\Zed\WebProfiler\Communication\Plugin\Form\WebProfilerFormPlugin;
class ExampleDependencyProvider
{
/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
protected function addProductSalePageWidgetPlugins($container): Container
{
$container->set(static::PYZ_PLUGIN_PRODUCT_SALE_PAGE_WIDGETS, function () {
return $this->getProductSalePageWidgetPlugins();
});
return $container;
}
/**
* @return array<string>
*/
protected function getProductSalePageWidgetPlugins(): array
{
return [
new WebProfilerFormPlugin()
];
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class SinglePlugin extends SprykerSinglePlugin
}
```

### Resolving the error
## Resolve the error

To resolve the error:

1. Remove the unused dead code in project.
Remove the unused dead code in project.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ The following is an example of the `composer.json` file when the used Spryker fe
}
```

### Resolving the error
## Resolving the error

To resolve this issue:

1. Update the outdated dependencies to make it correspond to the minimum required version.
Update the outdated dependencies to make it correspond to the minimum required version.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class ModuleDependencyProvider extends SprykerModuleDependencyProvider
}
```
### Resolving the error
## Resolving the error
To resolve the error:
1. Try to have simple configuration arrays. They shouldn't have more than two nesting levels inside.
Try to have simple configuration arrays. They shouldn't have more than two nesting levels inside.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ By using the npm vulnerabilities checker with the Evaluator, developers can safe
NPM CHECKER
===========

Message: [critical] Prototype pollution in webpack loader-utils
Message: [critical] Prototype pollution in webpack loader-utils
https://github.com/advisories/GHSA-76p3-8jx3-jpfq
Target: loader-utils

Expand All @@ -36,6 +36,6 @@ Message: [high] Cross-realm object access in Webpack 5
Read more: https://docs.spryker.com/docs/scos/dev/guidelines/keeping-a-project-upgradable/upgradability-guidelines/npm-checker.html
```

### Resolving the issue
## Resolve the error

To resolve the issue, update the npm dependencies with known vulnerabilities to the versions where the vulnerability issues are fixed.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Your `composer.lock` file contains package versions that have security issues:
...
````

### Resolving the error
## Resolve the error

To resolve the error:
1. Upgrade the package to a version where the vulnerability issue is fixed.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ image:

Inconsistent PHP versions produce the error message output.

### Resolving the error
## Resolve the error

To resolve the issue:
1. Use a supported [Spryker SDK](https://docs.spryker.com/docs/sdk/dev/spryker-sdk.html) PHP version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class CategoryDependencyProvider extends SprykerDependencyProvider
}
```

### Resolving the error
## Resolve the error

To solve this issue:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ConsoleDependencyProvider
}
```

### Resolving the error
## Resolve the error

To resolve the error:
1. Refactor the plugin - remove the usage of the complicated constructor arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ Your `composer.lock` file contains package versions that have security issues:
...
````

### Resolving the error
## Resolve the error
To resolve the error, upgrade the package to a version where the vulnerability issue is fixed.

0 comments on commit c008a3c

Please sign in to comment.