SourceTestAbstract   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 7
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testToOptionArray() 0 8 2
A testToArray() 7 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Meanbee\MagentoRoyalmail\Test\Unit\Model\Config;
4
5
use Magento\Framework\Option\ArrayInterface;
6
7
/**
8
 * Class SourceTest
9
 */
10
abstract class SourceTestAbstract extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * @var ArrayInterface|\PHPUnit_Framework_MockObject_MockObject
14
     */
15
    protected $source;
16
17
    public function testToOptionArray()
18
    {
19
        $options = $this->source->toOptionArray();
20
        foreach ($options as $option) {
21
            $this->assertArrayHasKey('value', $option);
22
            $this->assertArrayHasKey('label', $option);
23
        }
24
    }
25
26
    public function testToArray()
27
    {
28
        $array = $this->source->toArray();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Magento\Framework\Option\ArrayInterface as the method toArray() does only exist in the following implementations of said interface: Magento\Authorization\Mo...\Permissions\Collection, Magento\Authorization\Mo...ceModel\Role\Collection, Magento\Authorization\Mo...el\Role\Grid\Collection, Magento\Authorization\Mo...eModel\Rules\Collection, Magento\Backend\Model\Ca...ceModel\Grid\Collection, Magento\Backup\Model\Fs\Collection, Magento\CatalogRule\Mode...ceModel\Grid\Collection, Magento\CatalogRule\Mode...ceModel\Rule\Collection, Magento\CatalogRule\Mode...roduct\Price\Collection, Magento\CatalogUrlRewrit...egory\ProductCollection, Magento\Catalog\Model\Re...odel\AbstractCollection, Magento\Catalog\Model\Re...ry\Attribute\Collection, Magento\Catalog\Model\Re...del\Category\Collection, Magento\Catalog\Model\Re...ategory\Flat\Collection, Magento\Catalog\Model\Re...tion\AbstractCollection, Magento\Catalog\Model\Re...ct\Attribute\Collection, Magento\Catalog\Model\Re...odel\Product\Collection, Magento\Catalog\Model\Re...Compare\Item\Collection, Magento\Catalog\Model\Re...Product\Link\Collection, Magento\Catalog\Model\Re...Link\Product\Collection, Magento\Catalog\Model\Re...oduct\Option\Collection, Magento\Catalog\Model\Re...Option\Value\Collection, Magento\Cms\Model\ResourceModel\AbstractCollection, Magento\Cms\Model\ResourceModel\Block\Collection, Magento\Cms\Model\Resour...l\Block\Grid\Collection, Magento\Cms\Model\ResourceModel\Page\Collection, Magento\Cms\Model\Resour...el\Page\Grid\Collection, Magento\Cms\Model\Wysiwy...ages\Storage\Collection, Magento\Config\Model\Config\Source\Email\Template, Magento\Config\Model\Config\Source\Yesno, Magento\Config\Model\Res...\Config\Data\Collection, Magento\Cron\Model\Resou...del\Schedule\Collection, Magento\Customer\Model\Config\Share, Magento\Customer\Model\R...ss\Attribute\Collection, Magento\Customer\Model\R...odel\Address\Collection, Magento\Customer\Model\R...el\Attribute\Collection, Magento\Customer\Model\R...del\Customer\Collection, Magento\Customer\Model\R...rm\Attribute\Collection, Magento\Customer\Model\R...ceModel\Grid\Collection, Magento\Customer\Model\R...eModel\Group\Collection, Magento\Customer\Model\R...l\Group\Grid\Collection, Magento\Customer\Model\R...\Grid\ServiceCollection, Magento\Customer\Model\R...\Online\Grid\Collection, Magento\Customer\Model\R...odel\Visitor\Collection, Magento\Directory\Model\...odel\Country\Collection, Magento\Directory\Model\...untry\Format\Collection, Magento\Directory\Model\...Model\Region\Collection, Magento\Eav\Model\Entity...tion\AbstractCollection, Magento\Eav\Model\Entity...trol\AbstractCollection, Magento\Eav\Model\Resour...el\Attribute\Collection, Magento\Eav\Model\Resour...ty\Attribute\Collection, Magento\Eav\Model\Resour...tribute\Grid\Collection, Magento\Eav\Model\Resour...ribute\Group\Collection, Magento\Eav\Model\Resour...ibute\Option\Collection, Magento\Eav\Model\Resour...ttribute\Set\Collection, Magento\Eav\Model\Resour...\Entity\Type\Collection, Magento\Eav\Model\Resour...rm\Attribute\Collection, Magento\Eav\Model\Resour...Form\Element\Collection, Magento\Eav\Model\Resour...orm\Fieldset\Collection, Magento\Eav\Model\Resour...el\Form\Type\Collection, Magento\Eav\Test\Unit\Mo...\AbstractCollectionStub, Magento\Eav\Test\Unit\Mo...\AbstractCollectionStub, Magento\Email\Model\Reso...del\Template\Collection, Magento\Framework\Api\AbstractServiceCollection, Magento\Framework\Data\Collection, Magento\Framework\Data\Collection\AbstractDb, Magento\Framework\Data\Collection\Filesystem, Magento\Framework\Data\T...Collection\DbCollection, Magento\Framework\Model\...tion\AbstractCollection, Magento\Framework\Model\...rsionControl\Collection, Magento\Framework\Model\...Model\Db\Collection\Uut, Magento\Framework\Mview\View\Collection, Magento\Framework\View\E...taProvider\SearchResult, Magento\ImportExport\Mod...odel\History\Collection, Magento\ImportExport\Mod...Import\AbstractBehavior, Magento\ImportExport\Mod...e\Import\Behavior\Basic, Magento\ImportExport\Mod...\Import\Behavior\Custom, Magento\Indexer\Model\Indexer\Collection, Magento\Indexer\Model\Re...ndexer\State\Collection, Magento\Indexer\Model\Re...w\View\State\Collection, Magento\Integration\Mode...\Integration\Collection, Magento\Integration\Mode...uth\Consumer\Collection, Magento\Integration\Mode...\Oauth\Nonce\Collection, Magento\Integration\Mode...\Oauth\Token\Collection, Magento\Newsletter\Model...ceModel\Grid\Collection, Magento\Newsletter\Model...odel\Problem\Collection, Magento\Newsletter\Model...eModel\Queue\Collection, Magento\Newsletter\Model...l\Queue\Grid\Collection, Magento\Newsletter\Model...l\Subscriber\Collection, Magento\Newsletter\Model...scriber\Grid\Collection, Magento\Newsletter\Model...del\Template\Collection, Magento\PageCache\Model\...nfig\Source\Application, Magento\ProductAlert\Mod...eModel\Price\Collection, Magento\ProductAlert\Mod...eModel\Stock\Collection, Magento\Quote\Model\Reso...uote\Address\Collection, Magento\Quote\Model\Reso...Address\Item\Collection, Magento\Quote\Model\Reso...Address\Rate\Collection, Magento\Quote\Model\ResourceModel\Quote\Collection, Magento\Quote\Model\Reso...l\Quote\Item\Collection, Magento\Quote\Model\Reso...\Item\Option\Collection, Magento\Quote\Model\Reso...uote\Payment\Collection, Magento\Review\Model\Res...Model\Rating\Collection, Magento\Review\Model\Res...\Rating\Grid\Collection, Magento\Review\Model\Res...ating\Option\Collection, Magento\Review\Model\Res...\Option\Vote\Collection, Magento\Review\Model\Res...Model\Review\Collection, Magento\Review\Model\Res...eview\Status\Collection, Magento\Review\Model\Res...view\Summary\Collection, Magento\Rule\Model\Resou...tion\AbstractCollection, Magento\Security\Model\Config\Source\ResetMethod, Magento\Security\Model\R...nSessionInfo\Collection, Magento\Security\Model\R...RequestEvent\Collection, Magento\Store\Model\Reso...onfig\Collection\Scoped, Magento\Store\Model\ResourceModel\Group\Collection, Magento\Store\Model\ResourceModel\Store\Collection, Magento\Store\Model\Reso...odel\Website\Collection, Magento\Store\Model\Reso...Website\Grid\Collection, Magento\Theme\Model\Reso...Model\Design\Collection, Magento\Theme\Model\Reso...esign\Config\Collection, Magento\Theme\Model\Reso...Config\Scope\Collection, Magento\Theme\Model\ResourceModel\Theme\Collection, Magento\Theme\Model\Reso...l\Theme\Data\Collection, Magento\Theme\Model\Reso...l\Theme\File\Collection, Magento\Theme\Model\Reso...l\Theme\Grid\Collection, Magento\Theme\Model\Theme\Collection, Magento\Theme\Model\Theme\Data\Collection, Magento\Ui\Model\ResourceModel\Bookmark\Collection, Magento\UrlRewrite\Model...el\UrlRewriteCollection, Magento\User\Model\Resou...el\Role\User\Collection, Magento\User\Model\ResourceModel\User\Collection, Magento\User\Model\Resou...\User\Locked\Collection, Magento\User\Model\System\Config\Source\Password, Magento\Variable\Model\R...del\Variable\Collection, Magento\Widget\Model\Res...\Layout\Link\Collection, Magento\Widget\Model\Res...ayout\Update\Collection, Magento\Widget\Model\Res...get\Instance\Collection, Magento\Wishlist\Model\R...ceModel\Item\Collection, Magento\Wishlist\Model\R...el\Item\Collection\Grid, Magento\Wishlist\Model\R...\Item\Option\Collection, Magento\Wishlist\Model\R...del\Wishlist\Collection, Meanbee\MagentoRoyalmail...l\Config\Source\Methods, Meanbee\MagentoRoyalmail...onfig\Source\ParcelSize, Meanbee\MagentoRoyalmail...fig\Source\RoundingRule.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
29 View Code Duplication
        foreach ($array as $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
            $this->assertInternalType('array', $item);
31
            foreach ($item as $key => $value) {
32
                $this->assertInternalType('string', $key);
33
                $this->assertInstanceOf(\Magento\Framework\Phrase::class, $value);
34
            }
35
        }
36
    }
37
}
38