Skip to content
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

Don't make XML paths private. #29951

Closed
1 task
brideo opened this issue Sep 9, 2020 · 8 comments
Closed
1 task

Don't make XML paths private. #29951

brideo opened this issue Sep 9, 2020 · 8 comments
Assignees
Labels
duplicate Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Progress: done Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@brideo
Copy link
Contributor

brideo commented Sep 9, 2020

Summary (*)

I don't believe constants should be private as other implementations sometimes need to use them rather than redefining.

Examples (*)

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Theme\Block\Html;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Element\Template;
use Magento\Store\Model\ScopeInterface;

/**
 * Html page title block
 *
 * @method $this setTitleId($titleId)
 * @method $this setTitleClass($titleClass)
 * @method string getTitleId()
 * @method string getTitleClass()
 * @api
 * @since 100.0.2
 */
class Title extends Template
{
    /**
     * Config path to 'Translate Title' header settings
     */
    private const XML_PATH_HEADER_TRANSLATE_TITLE = 'design/header/translate_title';  // Why is this hidden? 

    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * Own page title to display on the page
     *
     * @var string
     */
    protected $pageTitle;

    /**
     * Constructor
     *
     * @param Template\Context $context
     * @param ScopeConfigInterface $scopeConfig
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        ScopeConfigInterface $scopeConfig,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->scopeConfig = $scopeConfig; // Can this be gotten from the $context object
    }

    /**
     * Provide own page title or pick it from Head Block
     *
     * @return string
     */
    public function getPageTitle()
    {
        if (!empty($this->pageTitle)) {
            return $this->pageTitle;
        }

        $pageTitle = $this->pageConfig->getTitle()->getShort();

        return $this->shouldTranslateTitle() ? __($pageTitle) : $pageTitle;
    }

    /**
     * Provide own page content heading
     *
     * @return string
     */
    public function getPageHeading()
    {
        $pageTitle = !empty($this->pageTitle) ? $this->pageTitle : $this->pageConfig->getTitle()->getShortHeading();

        return $this->shouldTranslateTitle() ? __($pageTitle) : $pageTitle;
    }

    /**
     * Set own page title
     *
     * @param string $pageTitle
     * @return void
     */
    public function setPageTitle($pageTitle)
    {
        $this->pageTitle = $pageTitle;
    }

    /**
     * Check if page title should be translated
     *
     * @return bool
     */
    private function shouldTranslateTitle(): bool
    {
        return $this->scopeConfig->isSetFlag(
            static::XML_PATH_HEADER_TRANSLATE_TITLE, // Why is this accessed using static if the constant is private?
            ScopeInterface::SCOPE_STORE
        );
    }
}

Proposed solution

  • do not make constants private
  • If constants are private, access them through self:: rather than static::

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
@brideo brideo added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Sep 9, 2020
@m2-assistant
Copy link

m2-assistant bot commented Sep 9, 2020

Hi @brideo. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Sep 9, 2020
@mrtuvn
Copy link
Contributor

mrtuvn commented Sep 9, 2020

you can access to path by self::XML_PATH_ instead static:XML_PATH
Seem your code is outdated @brideo
https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Theme/Block/Html/Title.php#L104
But i found this code fixed in mainline 2.3.5 branch
the fix not apply in released composer CE 2.4.0
CC @slavvka @ihor-sviziev

@hostep
Copy link
Contributor

hostep commented Sep 9, 2020

@brideo means that the const declaration should be public as it's a constant defining an xml path which can be of use to other modules to fetch configuration values. Right now you can't use the const outside of this class and you have to redefine it in your own module, which can be annoying.

I agree that all consts which define an xml path to a configuration should be public so we can easily access those from other modules.
If that really can't happen for some reason, then maybe every module in Magento which has one or more configuration settings should have public method(s) which can be used to fetch the particular configuration setting (but that will be a looooot of work).

@brideo
Copy link
Contributor Author

brideo commented Sep 9, 2020

I am running 2.4.0, which doesn't work because of this exact issue. I assume it can be closed because of #28981 which is this is a duplicate of.

@brideo
Copy link
Contributor Author

brideo commented Sep 9, 2020

Actually no sorry I'm wrong, I just looked through the commits. Why are we choosing to make constants unaccessible? They're useful for third party vendors, in fact I always thought it was best practise to access these values using constants?

@m2-assistant
Copy link

m2-assistant bot commented Oct 2, 2020

Hi @ihor-sviziev. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@ihor-sviziev
Copy link
Contributor

Hi @brideo,
Thank you for you report!
Currently we need to make such newly added constants as private for classes marked as @api due to backward compatibility.

Example: in some api marked class we’ll add a public constant and you as extension developer will start using it, also you would expect that your code should work fine on earlier magento versions, as you’re using the only the api classes. Unfortunately it will not work as the constant wasn’t present in earlier magento versions.

For newly created classes we should use public instead of private.

I hope I clearly described why it’s done in this way and we can close this issue.

@ihor-sviziev ihor-sviziev added duplicate Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch labels Oct 10, 2020
@ihor-sviziev
Copy link
Contributor

ihor-sviziev commented Oct 10, 2020

Hi @brideo,
I believe I clearly described above the issue. The exact case you've described was already fixed in #28797. If you'll find any other - let us know.

Also based on your suggestion I rule proposal for magento coding standards magento/magento-coding-standard#197.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Progress: done Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Archived in project
Development

No branches or pull requests

5 participants