<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ashish Ranade</title>
    <description>The latest articles on Forem by Ashish Ranade (@ashish11).</description>
    <link>https://forem.com/ashish11</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F373641%2F1f918c1d-d209-4b65-8823-17376d1a97ef.jpeg</url>
      <title>Forem: Ashish Ranade</title>
      <link>https://forem.com/ashish11</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ashish11"/>
    <language>en</language>
    <item>
      <title>Decomposition of Magento Controllers</title>
      <dc:creator>Ashish Ranade</dc:creator>
      <pubDate>Tue, 05 Jan 2021 10:17:48 +0000</pubDate>
      <link>https://forem.com/ashish11/decomposition-of-magento-controllers-42m0</link>
      <guid>https://forem.com/ashish11/decomposition-of-magento-controllers-42m0</guid>
      <description>&lt;p&gt;Controllers are the most important thing in MVC pattern. Controller helps to receive requests, process, and render pages. To create a front-end controller and to process the request, we need to extend our controller class from Magento\Framework\App\Action\Action. This class context gives some added features to handle the request type, and to validate authorized users and so on. But in this process Magento needs to include so many classes which ultimately increase execution time. &lt;/p&gt;

&lt;p&gt;To optimize this architecture community members came up with a great solution, i.e. decompositions of controllers.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the Decomposition of Magento?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://asolutions.co.in/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fasolutions.co.in%2Fwp-content%2Fuploads%2F2020%2F12%2FDecomposition-of-Magento-Controllers.png" alt="N|Solid"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Decomposition of Magento Controllers
&lt;/h2&gt;

&lt;p&gt;As a developer, we no longer need to extend Magento\Framework\App\Action\Action class, instead, we need to implement from \Magento\Framework\App\ActionInterface. If we know the purpose of our custom controller(whether it is use for GET, POST, etc operation) then there are separate interfaces that we can.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;\Magento\Framework\App\Action\HttpDeleteActionInterface&lt;br&gt;
\Magento\Framework\App\Action\HttpGetActionInterface&lt;br&gt;
\Magento\Framework\App\Action\HttpPostActionInterface&lt;br&gt;
\Magento\Framework\App\Action\HttpPutActionInterface&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php 
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\PageFactory;
class MyController implements HttpGetActionInterface
{
    /** @var PageFactory */
    protected $resultPageFactory;
    public function __construct(PageFactory $resultPageFactory)
    {
        $this-&amp;gt;resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {
        return $this-&amp;gt;resultPageFactory-&amp;gt;create();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that we have used \Magento\Framework\App\Action\HttpGetActionInterface. It is a method-specific Interface extending ActionInterface.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance
&lt;/h2&gt;

&lt;p&gt;In older version of Magento 2(till 2.3),  Magento were injecting the \Magento\Framework\App\Action\Context into Actions with a set of following classes:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;\Magento\Framework\App\RequestInterface&lt;br&gt;
\Magento\Framework\App\ResponseInterface&lt;br&gt;
\Magento\Framework\ObjectManagerInterface&lt;br&gt;
\Magento\Framework\Event\ManagerInterface&lt;br&gt;
\Magento\Framework\UrlInterface&lt;br&gt;
\Magento\Framework\App\Response\RedirectInterface&lt;br&gt;
\Magento\Framework\App\ActionFlag&lt;br&gt;
\Magento\Framework\App\ViewInterface&lt;br&gt;
\Magento\Framework\Message\ManagerInterface&lt;br&gt;
\Magento\Framework\Controller\Result\RedirectFactory&lt;br&gt;
\Magento\Framework\Controller\ResultFactory &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And these classes are no longer injected. It will significantly improve the performance and implementing a method specific interface is a much cleaner approach.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caution
&lt;/h2&gt;

&lt;p&gt;Keep in mind that some Modules have their own AbstractAction. For example, \Magento\Customer\Controller\AccountInterface additionally handles Customer Authentication.&lt;br&gt;
Controller “Supertypes” are deprecated (\Magento\Backend\App\AbstractAction, \Magento\Framework\App\Action\Action, \Magento\Framework\App\Action\AbstractAction, Magento\Framework\App\Action\Action\AbstractAccount) and you should not use them anymore.&lt;br&gt;
It is recommended to avoid code migration till 2.5.0 since 3-rd party observers may be subscribed to your controllers. Methods like getRequest, getResponse, getActionFlag are eliminated with the inheritance and it will lead to errors when accessing them through controller object from event.&lt;br&gt;
It is recommended to use a new approach for new code only starting 2.4.0 release.&lt;br&gt;
Existing Magento controllers will not be migrated until 2.5.0 to keep backward compatibility.&lt;br&gt;
Please let me know your thoughts about this blog through the comment section below, or write me an email on my email id i.e. &lt;a href="mailto:admin@asolutions.co.in"&gt;admin@asolutions.co.in&lt;/a&gt;. Till next time happy coding 🙂&lt;/p&gt;

</description>
      <category>magento2</category>
      <category>php</category>
      <category>controllers</category>
    </item>
    <item>
      <title>CSP In Magento 2</title>
      <dc:creator>Ashish Ranade</dc:creator>
      <pubDate>Thu, 24 Sep 2020 11:38:53 +0000</pubDate>
      <link>https://forem.com/ashish11/csp-in-magento-2-6n2</link>
      <guid>https://forem.com/ashish11/csp-in-magento-2-6n2</guid>
      <description>&lt;h1&gt;
  
  
  What is CSP?
&lt;/h1&gt;

&lt;p&gt;CSP is a standard introduced to prevent attacks on your website. Attacks mean an XSS, clickjacking, or malicious code injection. CSP is widely supported by all the modern web browsers and it gives privilege to website owners to approve origins of content that browsers should be allowed to load on that website. &lt;/p&gt;

&lt;p&gt;There are two ways that can be used to configure CSP for your website. The first one is by configuring your web server(apache or nGinx to set headers) or another method is to set &amp;lt;meta value on your website. Configuring web servers is beyond the scope of this blog post. Let’s take a look at how Magento configures CSP. &lt;/p&gt;

&lt;h1&gt;
  
  
  Magento and CSP
&lt;/h1&gt;

&lt;p&gt;CSP was introduced in Magento version 2.3.5 to prevent the loading of malicious scripts on websites. It helps to prevent websites from clickjacking, XSS(cross-site scripting) and session hijacking, and other attacks like that. Magento has a Magento_Csp module while helping to set the policy rules for adminhtml and for the frontend area separately to accommodate different use cases. CSP has two modes. The first one is to report only and another one is restrict mode.&lt;/p&gt;

&lt;h1&gt;
  
  
  CSP Modes
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Report Only Mode&lt;/strong&gt;: In this mode, Magento only reports the CSP policy violations errors in the console log but it will not act on those errors. It helps whenever you are in developer mode and needs to debug your functionality.&lt;br&gt;
Restrict Mode: In this mode, Magento acts on any policy violations. Magento will restrict the loading of CSS, JS, and inline script which violates the CSP policy. &lt;br&gt;
To configure report-only mode or restrict mode in your Magento application, you need to set the value in your etc/config.xml file.&lt;/p&gt;

&lt;p&gt;To enable the restrict mode we will set attribute value report_only to 1, or else if we need to set a report-only model set the report_only attribute value to 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Report Only Mode: report_only = 0&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Restrict Mode: report_only = 1&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"&amp;gt;
    &amp;lt;default&amp;gt;
        &amp;lt;csp&amp;gt;
            &amp;lt;mode&amp;gt;
                &amp;lt;storefront&amp;gt;
                    &amp;lt;report_only&amp;gt;0&amp;lt;/report_only&amp;gt;
                &amp;lt;/storefront&amp;gt;
                &amp;lt;admin&amp;gt;
                    &amp;lt;report_only&amp;gt;0&amp;lt;/report_only&amp;gt;
                &amp;lt;/admin&amp;gt;
            &amp;lt;/mode&amp;gt;
        &amp;lt;/csp&amp;gt;
    &amp;lt;/default&amp;gt;
&amp;lt;/config&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  How To Configure CSP for Custom Module
&lt;/h1&gt;

&lt;p&gt;We can add our whitelisted style, scripts, hashes domains in the csp_whitelist.xml file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp:etc/csp_whitelist.xsd"&amp;gt;
    &amp;lt;policies&amp;gt;
        &amp;lt;policy id="script-src"&amp;gt;
            &amp;lt;values&amp;gt;
                &amp;lt;value id="&amp;lt;your_unique_id&amp;gt;" type="host"&amp;gt;*.domain.com&amp;lt;/value&amp;gt;
                &amp;lt;value id="&amp;lt;your_unique_id&amp;gt;" type="host"&amp;gt;https://my-custom-domain.com&amp;lt;/value&amp;gt;
            &amp;lt;/values&amp;gt;
        &amp;lt;/policy&amp;gt;
        &amp;lt;policy id="connect-src"&amp;gt;
            &amp;lt;values&amp;gt;
                &amp;lt;value id="&amp;lt;your_unique_id&amp;gt;" type="host"&amp;gt;https://my-custom-domain-2.com&amp;lt;/value&amp;gt;
            &amp;lt;/values&amp;gt;
        &amp;lt;/policy&amp;gt;
    &amp;lt;/policies&amp;gt;
&amp;lt;/csp_whitelist&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have other types of CSP also, in the above example we have declared our whitelist domain under connect-src and script-src. The other types are.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POLICY NAME DESCRIPTION
default-src The default policy.
base-uri    Defines which URLs can appear in a page’s &amp;lt;base&amp;gt; element.
child-src   Defines the sources for workers and embedded frame contents.
connect-src Defines the sources that can be loaded using script interfaces.
font-src    Defines which sources can serve fonts.
form-action Defines valid endpoints for submission from &amp;lt;form&amp;gt; tags.
frame-ancestors Defines the sources that can embed the current page.
frame-src   Defines the sources for elements such as &amp;lt;frame&amp;gt; and &amp;lt;iframe&amp;gt;.
img-src Defines the sources from which images can be loaded.
manifest-src    Defines the allowable contents of web app manifests.
media-src   Defines the sources from which images can be loaded.
object-src  Defines the sources for the &amp;lt;object&amp;gt;, &amp;lt;embed&amp;gt;, and &amp;lt;applet&amp;gt; elements.
script-src  Defines the sources for JavaScript &amp;lt;script&amp;gt; elements.
style-src   Defines the sources for stylesheets.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the domains are not whitelisted for your script and styles, you may see the following error like “[Report Only] Refused to load the script ‘’ because it violates the following Content Security Policy directive” in your developer console. To resolve this issue we need to add that domain under the whitelisted domain using the csp_whilelist.xml file.&lt;/p&gt;

&lt;h1&gt;
  
  
  Inline Script Validation
&lt;/h1&gt;

&lt;p&gt;To validate our in-line script, styles, or hashes we can use the SecureHtmlRenderer class from Magento\Framework\View\Helper. We can create an object of this class and use the renderTag method to whitelist our inline script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer */
private $secureRenderer;
function someMethod() {
   ....
   $html .= $this-&amp;gt;secureRenderer-&amp;gt;renderTag('style', [], "#element { color: blue } ", false);
   ....
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, that is implementation from your block class, let’s say if we need to use the same implementation in your phtml file then we need to create a function in your custom block class and pass the SecureHtmlRenderer object through the public method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="alert-div"&amp;gt;Some text value&amp;lt;/div&amp;gt;
&amp;lt;?= $secureRenderer-&amp;gt;renderEventListenerAsTag('onclick', 'alert("Whitelist me!");', '#alert-div'); ?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is all about CSP in Magento 2. Please let me know what you think about this post through the comment section or you can write me an email on my email id i.e. &lt;a href="mailto:admin@asolutions.co.in"&gt;admin@asolutions.co.in&lt;/a&gt;. Happy coding :). Thanks!&lt;/p&gt;

</description>
      <category>csp</category>
      <category>magento2</category>
    </item>
    <item>
      <title>Profiler in Magento 2</title>
      <dc:creator>Ashish Ranade</dc:creator>
      <pubDate>Sat, 19 Sep 2020 15:59:42 +0000</pubDate>
      <link>https://forem.com/ashish11/profiler-in-magento-2-15m1</link>
      <guid>https://forem.com/ashish11/profiler-in-magento-2-15m1</guid>
      <description>&lt;p&gt;Profiler in simple language can be considered as a program, which helps to check and report the performance of another program. Magento 2 is also using a profiler for the same purpose. If we need to check the performance of any specific script or need to check the performance of a complete page, we can simply enable the profiler and look at the output. Magento 2 provides 2 types of profiles i.e. HTML, CSV.&lt;/p&gt;

&lt;p&gt;Enable/Disable profiler&lt;br&gt;
There are two ways to enable profiler in Magento 2. The first way is to enable the profiler using the SetEnv variable, we can set this variable in .htaccess like &lt;/p&gt;

&lt;p&gt;&lt;code&gt;SetEnv MAGE_PROFILER &amp;lt;type&amp;gt;&lt;/code&gt;&lt;br&gt;
Where  can be one of the following HTML or CSV based on your need, or we can set the value at the bootstrapping of Magento application through apache or Nginx servers. If you guys need more information about setting up variables at bootstrapping, please check out this link for more information. &lt;/p&gt;

&lt;p&gt;To disable the profile, just comment a SetEnv MAGE_PROFILER from your .htaccess file or from your web server config and restart your web server. &lt;/p&gt;

&lt;p&gt;There is another way to enable profiler in Magento 2 i.e. using a CLI. &lt;/p&gt;

&lt;p&gt;To enable profiler, navigate to the Magento installation directory as the Magento file system owner, enter the following command to configure the profiler. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;php bin\magento dev:profiler:enable &amp;lt;type&amp;gt;&lt;/code&gt;&lt;br&gt;
Where  can be one of the following HTML or CSV based on your need. To disable the profiler, execute the following command from your Magento root directory. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;php bin/magento dev:profiler:disable&lt;/code&gt;&lt;br&gt;
Once we enable the profiler in Magento, the application creates a flag file inside  &lt;code&gt;&amp;lt;magento_root&amp;gt;/var&lt;/code&gt; directory as profiler.flag, this file contains the name of profiler type that we have set through CLI or MAGE_PROFILER variable. &lt;/p&gt;

&lt;h1&gt;
  
  
  HTML Profiler
&lt;/h1&gt;

&lt;p&gt;We can enable the HTML profiler using the following command &lt;/p&gt;

&lt;p&gt;&lt;code&gt;php bin\magento dev:profiler:enable html&lt;/code&gt;&lt;br&gt;
This command shows the output in your web browser at the bottom of your page. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Just make sure that you are not using varnish or any other caching mechanism or disable before using the Magento profiler. &lt;/p&gt;

&lt;p&gt;If you have enabled the “html” type profiler, then it always display the result on the footer section of a web page. You can see in the following image:&lt;/p&gt;

&lt;p&gt;Once we enable the &lt;code&gt;dev:profiler:enable&lt;/code&gt; without specifying any  param, Magento set HTML as a default profiler type. &lt;/p&gt;

&lt;p&gt;We can find more detail about this command in Console/Command/ProfilerEnableCommand.php a class file where &lt;code&gt;TYPE_DEFAULT&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Is set as HTML. The output driver class resides under Magento’s profiler module i.e. Magento\Framework\Profiler\Driver\Standard\Output&lt;/p&gt;

&lt;h1&gt;
  
  
  CSV Profiler
&lt;/h1&gt;

&lt;p&gt;CSV profiler type, it gives another way to check the performance of our application. When we trigger a CLI command &lt;code&gt;dev:profiler:enable csvfile&lt;/code&gt; the output will be saved to &lt;code&gt;&amp;lt;project_root&amp;gt;/var/log/profiler.csv&lt;/code&gt;. The profiler.csv will be overridden on each page refresh. The output driver class resides under Magento’s profiler module i.e. Profiler/Driver/Standard/Output/Csvfile.php&lt;/p&gt;

&lt;p&gt;In output profiler, the columns are as follows: (you can find this variable defined in Profiler/Driver/Standard/Stat.php class as constant variables)&lt;/p&gt;

&lt;p&gt;Timer Id: This gives the name of the block of code being executed.&lt;br&gt;
Time: The time it took to complete in seconds.&lt;br&gt;
Avg: The average time it took to complete in seconds.&lt;br&gt;
Count: This represents the number of times this individual block ran to generate the output required.&lt;br&gt;
Emalloc: The amount of memory PHP ( the programming language on which Magento runs) assigned to this single operation. This is again represented in bytes.&lt;br&gt;
RealMem: The actual amount of memory used to perform the operation.&lt;br&gt;
This is all about Magento profiling. Please let me know your thoughts about this blog down in the comment section, or, write me an email on &lt;a href="mailto:admin@asolutions.co.in"&gt;admin@asolutions.co.in&lt;/a&gt; . That’s all for now, Happy coding &lt;/p&gt;

</description>
      <category>profiler</category>
      <category>magento2</category>
    </item>
    <item>
      <title>Code Quality Check in Magento 2</title>
      <dc:creator>Ashish Ranade</dc:creator>
      <pubDate>Thu, 30 Jul 2020 12:07:04 +0000</pubDate>
      <link>https://forem.com/ashish11/code-quality-check-in-magento-2-43aa</link>
      <guid>https://forem.com/ashish11/code-quality-check-in-magento-2-43aa</guid>
      <description>&lt;h1&gt;
  
  
  What is GrumPHP?
&lt;/h1&gt;

&lt;p&gt;GrumPHP is a composer library which helps to check the code quality at the time of code commit. GrumPHP creates a git hook before your commit and validates the code quality using the set of rules provided by us(in this case a set of rules provided by Magento). It helps to write the same quality of code as Magento core developers.&lt;/p&gt;

&lt;h1&gt;
  
  
  Installation of GrumPHP
&lt;/h1&gt;

&lt;p&gt;I am assuming that you have already installed Magento’s latest version and composer into your local system. Now, let’s open our terminal or command prompt, navigate to our Magento root directory and run the following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;composer require --dev phpro/grumphp&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command will help us to install grumphp with all the dependencies. At the end of the installation, GrumPHP will ask to create a grumphp.yml file type ‘yes/y’ and then in the next step, we need to select the coding standard that we need to follow. For our Magento project select PHPCS. &lt;/p&gt;

&lt;p&gt;Now, we need to install other required dependencies like phpcs and phpmd. PHPcs stands for PHP code-sniffer whereas phpmd stands for PHP mess detector.&lt;/p&gt;

&lt;p&gt;Execute the following command from our terminal to install these dependencies.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;composer require --dev squizlabs/php_codesniffer phpmd/phpmd&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Once both the dependencies are installed successfully,  it’s time to set our coding standards in a grumphp.yml file. Open grumphp.yml file in any text editor and add the following content.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# grumphp.yml&lt;br&gt;
grumphp:&lt;br&gt;
   tasks:&lt;br&gt;
       phpcs:&lt;br&gt;
           standard: "dev/tests/static/framework/Magento/ruleset.xml"&lt;br&gt;
           tab_width: ~&lt;br&gt;
           ignore_patterns: []&lt;br&gt;
       phpmd:&lt;br&gt;
           exclude: []&lt;br&gt;
           ruleset: ['dev/tests/static/testsuite/Magento/Test/Php/_files/phpmd/ruleset.xml']&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Here, inside the tasks option, we have set the standards for PHPcs and PHPmd. We can add different sets of rules based on our requirements.&lt;/p&gt;

&lt;p&gt;Now, to check that our grumphp is working or not, navigate to our Magento root directory and check the git status for any changes using the following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git status&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now add the file that we need to commit using the following command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Git add &amp;lt;your-file-name&amp;gt;&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;You will not get any error or warning message this point of time, because the grumphp hook only works on before commit. Let’s commit our code using the following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Git commit -m &amp;lt;your-commit-message&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once we execute this git commit command the code is validated by the coding standard that we have set inside the grumphp.yml file and it will throw an error message.&lt;/p&gt;

&lt;p&gt;Once you resolve all the issues and code quality matches with the standard, you will be able to see the success message.&lt;/p&gt;

&lt;p&gt;Grumphp adds a hook before your commit and terminates the process to restrict the code from being committed. This will help to match the code quality and maintain the coding standards as core Magento developers. Your colleagues will also learn to write better code according to the best practices of your team.&lt;/p&gt;

</description>
      <category>magento2</category>
      <category>grumphp</category>
      <category>magento</category>
      <category>php</category>
    </item>
    <item>
      <title>Scalar Type Declaration</title>
      <dc:creator>Ashish Ranade</dc:creator>
      <pubDate>Mon, 20 Jul 2020 16:16:12 +0000</pubDate>
      <link>https://forem.com/ashish11/scalar-type-declaration-a28</link>
      <guid>https://forem.com/ashish11/scalar-type-declaration-a28</guid>
      <description>&lt;p&gt;Today, I like to share some of the key features provided in PHP. The PHP community has introduced some good features like Scalar type declaration, Return type declarations, etc.., which add some extra features to the PHP development, and provides flexibility in the development lifecycle. In this blog, I would like to explain the Scalar type declaration feature, I hope this will help you to improve your knowledge and help you somehow in the future.&lt;/p&gt;

&lt;h1&gt;
  
  
  Scalar Type Declaration
&lt;/h1&gt;

&lt;p&gt;Type declaration is also known as Type Hinting, allows a function to accept a parameter of a certain type if the given value is not of the correct type (Integer, Float, Boolean, string) PHP will return a fatal error. The older version of PHP (PHP 5) gives a recoverable error while the new release (PHP 7) returns a throwable error.&lt;/p&gt;

&lt;p&gt;PHP 7 has introduced the following Scalar type declarations.&lt;/p&gt;

&lt;p&gt;1.Boolean&lt;br&gt;
2.String&lt;br&gt;
3.Float&lt;br&gt;
4.Integer&lt;br&gt;
5.Iterable&lt;br&gt;
Scalar Type Declaration comes in two types Coercive(i.e. default), and another one is Strict, if we want to enforce PHP to use a strict type declaration then, we should set the strict value to 1 using a declare directive, please check the example below.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;declare(strict_types=1);&lt;/code&gt;&lt;br&gt;
Please check the example below for Coercive and Strict type declarations.&lt;/p&gt;

&lt;h1&gt;
  
  
  Coercive Scalar Type Declaration
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try{
     function sum(int …$ints) 
     {
         return array_sum($ints);
     }
}catch(Exception $ex)
{
   echo $ex-&amp;gt;getMessage();
}
print(sum(2, ‘3’, 4.1));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example just want to highlight one thing i.e. we are not using a strict value for parameter type like 2 of Integer type, 3 of String type 4.1 of Float type. The above code will output the value int(9).&lt;/p&gt;

&lt;h1&gt;
  
  
  Strict Scalar Type Declaration
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;declare(strict_types=1);
try {
      function sum(int …$ints) 
      {
         return array_sum($ints);
      }
} catch (Exception $ex) 
{   
   echo $ex-&amp;gt;gt;
   getMessage();
}
print(sum(2, ‘3’, 4.1));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that, if we declare the &lt;code&gt;strict_type&lt;/code&gt; value to 1, the above code will output “Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string is given”.&lt;/p&gt;

&lt;p&gt;The above code will help us to enforce the other developer to use a variable of strict type i.e. Integer, Float, etc.&lt;/p&gt;

&lt;h1&gt;
  
  
  Use of Scalar Type Declaration
&lt;/h1&gt;

&lt;p&gt;The scalar type declaration can be used in different areas of development, like, defining strict values in an interface, so that, the child class which is overriding the interface must follow the strict type.&lt;/p&gt;

&lt;p&gt;I hope that will help you somehow. Feel free to leave a comment below. Let me know what you think about this&lt;/p&gt;

</description>
      <category>php</category>
    </item>
  </channel>
</rss>
