I've built a CRUD module in Magento2.
My Save
admin controller has 2 dependencies that are basically the same classes, with different settings.
But when I run the static tests php bin/magento dev:tests:run static
I get an error message saying that this is wrong. I should not the same class as a dependency twice for one class.
Data set: array (
0 => '\\Sample\\News\\Controller\\Adminhtml\\Author\\Save',
)
Argument type duplication in class Sample\News\Controller\Adminhtml\Author\Save in /[ROOT]/app/code/Sample/News/Controller/Adminhtml/Author/Save.php
Multiple type injection [\Sample\News\Model\Uploader]
But I kind of need it like that.
I have to upload 2 things into my entity. An image and a file.
The upload for these 2 behave the same up to only one point, so I have a common class for them, but they are a bit different.
So my controller constructor looks like this:
public function __construct(
...
\Sample\News\Model\Uploader $fileUploader,
\Sample\News\Model\Uploader $imageUploader,
....
)
{
....
$this->fileUploader = $fileUploader;
$this->imageUploader = $imageUploader;
...
}
and in di.xml
I have the difference between these 2 classes.
<virtualType name="SampleNewsAuthorImageUploader" type="Sample\News\Model\Uploader"><!-- Virtual type for uploading images -->
<arguments>
<argument name="baseTmpPath" xsi:type="const">Sample\News\Model\Uploader::IMAGE_TMP_PATH</argument>
<argument name="basePath" xsi:type="const">Sample\News\Model\Uploader::IMAGE_PATH</argument>
<argument name="allowedExtensions" xsi:type="array">
<item name="jpg" xsi:type="string">jpg</item>
<item name="jpeg" xsi:type="string">jpeg</item>
<item name="gif" xsi:type="string">gif</item>
<item name="png" xsi:type="string">png</item>
</argument>
</arguments>
</virtualType>
<virtualType name="SampleNewsAuthorFileUploader" type="Sample\News\Model\Uploader"><!-- virtual type for any file upload -->
<arguments>
<argument name="baseTmpPath" xsi:type="const">Sample\News\Model\Uploader::FILE_TMP_PATH</argument>
<argument name="basePath" xsi:type="const">Sample\News\Model\Uploader::FILE_PATH</argument>
<argument name="allowedExtensions" xsi:type="array" />
</arguments>
</virtualType>
<type name="Sample\News\Controller\Adminhtml\Author\Save">
<arguments><!-- assigne above classes as dependencies for my controller -->
<argument name="fileUploader" xsi:type="object">SampleNewsAuthorFileUploader</argument>
<argument name="imageUploader" xsi:type="object">SampleNewsAuthorImageUploader</argument>
</arguments>
</type>
Is there a way to avoid the duplication of the same dependency or is there an annotation I can use so I won't see the error when I run the tests?
Similar to @SuppressWarnings(PHPMD.ExcessiveMethodLength)
.
(basically I want to hide the dirt under the rug if I cannot do it properly).
\Proxy
to one of the dependencies :)