Skip to content

Add type declaration to setter methods of generated ExtensionAttribute classes #2451

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

Conversation

erikhansen
Copy link
Contributor

The code that generates the classes that implement the \Magento\Framework\Api\ExtensionAttributesInterface interface were doing so in a way that did not add type declarations to the setter methods. This pull request adds functionality to add type declarations, but only for types that are valid type declarations. It is important to have type declarations added to the setter methods of ExtensionAttribute classes as it ensures that the getter methods will always return objects of the proper interface/class.

Summary of changes:

  • Modified \Magento\Framework\Api\Code\Generator\ExtensionAttributesGenerator::_getClassMethods method to add type declaration to method, as long as type is a valid (e.g. not a primitive type or an array of classes/interfaces).
  • Added isValidTypeDeclaration method to \Magento\Framework\Reflection\TypeProcessor to centralize the logic for determining whether a string is valid as a type declaration. When and if Magento 2 supports PHP 7, the logic of this method should be changed to support primitive types. I referenced the PHP Type Declaration section when writing the \Magento\Framework\Reflection\Test\Unit\TypeProcessorTest::testIsValidTypeDeclaration test. I wrote what I believe to be a very comprehensive set of true and false assertions.

Here is an example of how this auto-generated file changes: var/generation/Magento/Catalog/Api/Data/ProductAttributeMediaGalleryEntryExtension.php.

Before pull request

<?php
namespace Magento\Catalog\Api\Data;

/**
 * Extension class for @see
 * \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface
 */
class ProductAttributeMediaGalleryEntryExtension extends \Magento\Framework\Api\AbstractSimpleObject implements \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtensionInterface
{
    /**
     * @return \Magento\Framework\Api\Data\VideoContentInterface|null
     */
    public function getVideoContent()
    {
        return $this->_get('video_content');
    }

    /**
     * @param \Magento\Framework\Api\Data\VideoContentInterface $videoContent
     * @return $this
     */
    public function setVideoContent($videoContent)
    {
        $this->setData('video_content', $videoContent);
        return $this;
    }
}

After pull request

You'll notice that the setVideoContent method has a \Magento\Framework\Api\Data\VideoContentInterface type declaration.

<?php
namespace Magento\Catalog\Api\Data;

/**
 * Extension class for @see
 * \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface
 */
class ProductAttributeMediaGalleryEntryExtension extends \Magento\Framework\Api\AbstractSimpleObject implements \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtensionInterface
{
    /**
     * @return \Magento\Framework\Api\Data\VideoContentInterface|null
     */
    public function getVideoContent()
    {
        return $this->_get('video_content');
    }

    /**
     * @param \Magento\Framework\Api\Data\VideoContentInterface $videoContent
     * @return $this
     */
    public function setVideoContent(\Magento\Framework\Api\Data\VideoContentInterface $videoContent)
    {
        $this->setData('video_content', $videoContent);
        return $this;
    }
}

@Vinai Vinai added the PS label Nov 23, 2015
@Vinai
Copy link
Contributor

Vinai commented Nov 23, 2015

👍 Hopefully authentication for travis builds will be fixed soon, so the PR can be processed further.

@erikhansen erikhansen force-pushed the feature/add-type-declaration-extension-attribute branch from 002ba2b to 2d5ea54 Compare November 25, 2015 22:14
@vancoz
Copy link

vancoz commented Jan 12, 2016

Please merge latest from develop and rerun builds.

@erikhansen erikhansen force-pushed the feature/add-type-declaration-extension-attribute branch 2 times, most recently from bf9b759 to 29b57bd Compare January 14, 2016 19:04
@erikhansen
Copy link
Contributor Author

@vancoz It looks like the tests are failing because there is core code that is setting null values on methods that are now expecting objects of a certain type: PHP Catchable fatal error: Argument 1 passed to Magento\Sales\Api\Data\OrderPaymentExtension::setVaultPaymentToken() must implement interface Magento\Vault\Api\Data\PaymentTokenInterface, null given, called in /home/travis/build/magento/magento2/app/code/Magento/Vault/Plugin/PaymentVaultAttributesLoad.php on line 62 and defined in /home/travis/build/magento/magento2/dev/tests/integration/tmp/sandbox-0-f56255a0efbdc640bbc76576a01f0349/var/generation/Magento/Sales/Api/Data/OrderPaymentExtension.php on line 21

(From https://travis-ci.org/magento/magento2/jobs/102137070#L587)

Can you have one of your architects review this pull request and confirm that they would like to accept this pull request, assuming the build is green? If so, then I can spend the time to refactor any core Magento code that tries to use an extension attribute method to set a value that isn't an instanceof the proper interface. But I only want to spend this time if this PR is going to be merged.

My refactoring will look for any places where a null value could be passed to an extension attribute method that expects an object that implements the defined interface, and if so, it won't try to set a value.

@okorshenko okorshenko self-assigned this Feb 17, 2016
@okorshenko
Copy link
Contributor

Change makes sense, thanks for contribution! Could you please fix Travis builds so that we will be able accept this PR.

@erikhansen
Copy link
Contributor Author

@okorshenko Are you confirming that you would accept this PR if I make the changes I mentioned on January 14th?

@okorshenko
Copy link
Contributor

@erikhansen , yes, I do

@okorshenko
Copy link
Contributor

PR is Accepted, but tests should be fixed before merge

@sshrewz sshrewz added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Feb 26, 2016
@okorshenko
Copy link
Contributor

Hi @erikhansen. Should we wait for tests fixes for this Pull Request or this PR can be closed?

Erik Hansen and others added 2 commits March 21, 2016 21:16
- It is important to have type declarations added to the setter methods
  of ExtensionAttribute classes as it ensures that the getter methods will
  always return objects of the proper interface/class
- Added isValidTypeDeclaration method to \Magento\Framework\Reflection\TypeProcessor
  to centralize the logic for determine whether a string is valid as a
  type declaration. When and if Magento 2 supports PHP 7, the logic of
  this method should be changed to support primitive types.
- Tests updated/added to reflect changes
@erikhansen erikhansen force-pushed the feature/add-type-declaration-extension-attribute branch from 29b57bd to dd90e3a Compare March 22, 2016 02:18
@erikhansen
Copy link
Contributor Author

@okorshenko I just fixed the issue with the test that failed back in January. I'll see what additional tests fail and resolve those issue, assuming the fixes are not too intensive.

@erikhansen erikhansen force-pushed the feature/add-type-declaration-extension-attribute branch from 0d293e3 to a56b768 Compare March 22, 2016 20:44
@erikhansen
Copy link
Contributor Author

@okorshenko Travis tests are now green, so this PR should be ready to merge into develop.

@erikhansen
Copy link
Contributor Author

@okorshenko Bump.

@okorshenko
Copy link
Contributor

Hi @erikhansen . We are running internal builds against your branch.

@okorshenko
Copy link
Contributor

@erikhansen we successfully executed all builds against your branch. There are couple issues that we need to fix before merging this PR.

@erikhansen
Copy link
Contributor Author

@okorshenko Ok, good. Do I need to fix the issues or are you going to take care of them? If the former, can you let me know what the issues are?

}

/**
* @param \Magento\Bundle\Api\Data\BundleOptionInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

param annotation should looks like:

  • @param \Magento\Bundle\Api\Data\BundleOptionInterface $complexObjectAttributeWithTypeDeclaration

but this is not issue of this PR

@okorshenko
Copy link
Contributor

@erikhansen I added some comments to show you what issue we found in this PR. We will fix this and and will run all tests again. Thank you for your contribution.

@adragus-inviqa
Copy link
Contributor

@okorshenko - Interesting approach to keeping BC, I must say. But it works.

Slightly offtopic here, but do M2 devs agree with the whole Interface2, Interface3 approach in case of interface changes (the above snippet being for concrete classes only)?

@okorshenko
Copy link
Contributor

@erikhansen your pull request merged to develop branch. Thank you for your contribution.

@erikhansen erikhansen deleted the feature/add-type-declaration-extension-attribute branch May 7, 2016 16:50
magento-engcom-team pushed a commit that referenced this pull request Apr 25, 2018
[PERFORMANCE] Use price index to calculate price for configurable products
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Progress: accept
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants