Skip to content

#26: Add Sniff for Getters not change state #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

larsroettig
Copy link
Member

see #26

@larsroettig larsroettig changed the title #26: Add Sniff for Gettersnot change state #26: Add Sniff for Getters not change state Mar 8, 2019
@larsroettig larsroettig requested a review from lenaorobei March 8, 2019 13:17
@lenaorobei lenaorobei requested a review from paliarush March 8, 2019 13:32
@lenaorobei
Copy link
Contributor

Could you please commit ruleset.xml as well.

@larsroettig
Copy link
Member Author

@lenaorobei done 😸

lenaorobei
lenaorobei previously approved these changes Mar 8, 2019
lenaorobei
lenaorobei previously approved these changes Mar 8, 2019
paliarush
paliarush previously approved these changes Mar 8, 2019
@lenaorobei lenaorobei added the event: MageTestFest2019 MageTestFest contributions label Mar 8, 2019
@lenaorobei lenaorobei dismissed stale reviews from paliarush and themself March 8, 2019 15:52

Need to take property caching into account.

@larsroettig
Copy link
Member Author

larsroettig commented Mar 8, 2019

Hi @lenaorobei,
if we change this check to the only method should change the class property. We break a very typical Facade design pattern this will not work any more or marked as wrong but it can be valid.

Conditions:

  • My SizeService has very complex logic set and get sizes very expensively.

Possbily Solutions

  1. We should check for the if block is in there assignment this is okay caching reason.
  2. We ignore with set or __construct prefix for property assignments check.

See my Example:

<?php

namespace Foo\Bar;

class SizeService
{
    /**
     * @return int
     */
    public function getSize()
    {
        // some implementation
        return 12;
    }

    /**
     * @param $size
     * @return void
     */
    public function setSize($size)
    {
        // some implementation
    }
}

interface SizeServiceInterface
{
    /**
     * @return int
     */
    public function getSize();

    /**
     * @param $size
     * @return void
     */
    public function setSize($size);
}


class SizeServiceFacade implements SizeServiceInterface
{
    /**
     * @var int
     */
    private $size;

    /**
     * @var SizeService
     */
    private $service;

    /**
     * @param SizeService $service
     */
    public function __construct(SizeServiceInterface $service)
    {
        $this->service = $service;
    }

    /**
     * @return int
     */
    public function getSize()
    {
        if ($this->size === null) {
            $this->size = $this->service->getSize();
        }

        return $this->size;
    }

    /**
     * @return void
     */
    public function setSize($size)
    {
        $this->size = $size;
        $this->service->setSize($size);
    }
}

@larsroettig
Copy link
Member Author

Hi @paliarush and @lenaorobei,
I think we get too many false positives it is very hard to Implementing this with tokens maybe with phpstan it is possible to implement this check.

Testcase:

<?php

namespace Foo\Bar;


abstract class Bar
{
    public static $foobar = 100;
}

class Foo extends Bar
{
    /**
     * @var int
     */
    private static $staticProperty;

    /**
     * @var int
     */
    private $property;

    /**
     * @return int
     */
    public function getStaticProperty()
    {
        self::$staticProperty = 12;
        static::$staticProperty -= 12;
        self::$staticProperty .= 12;
        return self::$staticProperty;
    }

    /**
     * @return int
     */
    public function getProperty()
    {
        if (true) {

        }

        $this->property = 1223;
        return $this->property;
    }

    /**
     * @return int
     */
    public function getPropertyCached()
    {
        if ($this->property === null) {
            $this->property = 1223;
        }

        return $this->property;
    }

    public function getPropertyLocal()
    {
        $local = $this->property;
        $localArray = [
            'payment' => [
                'test' => [
                    'isActive' => $this->config->isActive(),
                    'title' => $this->config->getTitle()
                ]
            ]
        ];
        return $this->property;
    }

    private function getSalesChannelForOrder($order)
    {
        $websiteId = (int)$order->getStore()->getWebsiteId();
        $websiteCode = $this->websiteRepository->getById($websiteId)->getCode();

        return $this->salesChannelFactory->create([
            'data' => [
                'type' => '',
                'code' => $websiteCode
            ]
        ]);
    }

    const MODE_AUTO = 0;

    const MODE_MANUAL = 1;

    public function getOptionsArray()
    {
        return [
            self::MODE_AUTO => __('Automatically'),
            self::MODE_MANUAL => __('Manually')
        ];
    }

    public function testigetFoo()
    {
        $this->property = 1223;
        return $this->property;
    }

    /**
     * @return int
     */
    public function normalMethod()
    {
        $localVariable = 12;
        return $localVariable;
    }

    public function getStorageModel($storage = null, $params = [])
    {
        if ($storage === null) {
            $storage = $this->_coreFileStorage->getCurrentStorageCode();
        }

        switch ($storage) {
            case self::STORAGE_MEDIA_FILE_SYSTEM:
                $model = $this->_fileFactory->create();
                break;
            default:
                return false;
        }

        if (isset($params['init']) && $params['init']) {
            $model->init();
        }

        return $model;
    }
}

$d = function ($test) {
    $test = 123;
};

@lenaorobei lenaorobei deleted the 26-getter-sniff branch March 18, 2019 15:25
magento-devops-reposync-svc pushed a commit that referenced this pull request Sep 7, 2021
…-coding-standard-264

[Imported] Version 9 master update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants