{"id":2956,"date":"2022-06-24T11:26:23","date_gmt":"2022-06-24T09:26:23","guid":{"rendered":"https:\/\/nyamsprod.com\/blog\/?p=2956"},"modified":"2022-06-24T11:26:23","modified_gmt":"2022-06-24T09:26:23","slug":"type-hint-and-decoupling","status":"publish","type":"post","link":"https:\/\/nyamsprod.com\/blog\/type-hint-and-decoupling\/","title":{"rendered":"Type hint and Decoupling"},"content":{"rendered":"<div class=\"message warning\">\n<p><strong>Attention:<\/strong> Les informations de ce billet sont susceptibles d'&ecirc;tre obsol&egrave;tes car vieux de plus 2 ans.<\/p>\n<p><strong>Warning: <\/strong> The information you are reading may be obsolete, this post was published more than 2 years ago.<\/p>\n<\/div>\n<p>While type hinting is important to help improve your codebase usage and testing. For all the fun and joy it brings it can also lead to some of the worst maintenance hell if it is not done correctly. As one famous Uncle Ben used to say with great power comes great responsibility so here&#8217;s a quick reminder on how to properly type-hint.<\/p>\n\n\n\n<p>To illustrate this, I will use the <code>Carbon<\/code> package. This is in no way shape or form a criticism of the package but I think it&#8217;s usage makes it a great use case:<\/p>\n\n\n\n<p>Let&#8217;s say we want to change a date. I&#8217;m pretty sure that this piece of code  was recently added to a codebase near you. <\/p>\n\n\n<script src=\"https:\/\/gist.github.com\/af49f73749f2a61e7618d844d8f4f1f3.js?file=mutable-function-carbon.php\"><\/script>\n\n\n\n<p><code>Carbon\\Carbon<\/code> is a mutable child of PHP&#8217;s <code>DateTime<\/code> class. In 2022, we should stop using <code>DateTime<\/code>. The class is mutable which means that the number of unpredictable things, this piece of code can do in your codebase is frightening.<\/p>\n\n\n\n<p>Thanksfully for us, the code can be improved by just doing this.<\/p>\n\n\n<script src=\"https:\/\/gist.github.com\/af49f73749f2a61e7618d844d8f4f1f3.js?file=mutable-function-carbonimmutable.php\"><\/script>\n\n\n\n<p>This way side effect are prevented as <code>Carbon\\CarbonImmutable<\/code> is an immutable child of PHP&#8217;s <code>DateTimeImmutable<\/code> class. Last but not least, if this method is part of an interface or of the public API, I would instead suggest doing this:<\/p>\n\n\n<script src=\"https:\/\/gist.github.com\/af49f73749f2a61e7618d844d8f4f1f3.js?file=mutable-interface-datetimeimmutable.php\"><\/script>\n\n\n\n<p>This way the public API no longer rely on a specific PHP package (it&#8217;s decoupled), only this specific implementation does which is OK.<\/p>\n\n\n\n<p>But what about when you do not need to mutate your object ?<\/p>\n\n\n<script src=\"https:\/\/gist.github.com\/af49f73749f2a61e7618d844d8f4f1f3.js?file=mutable-query-carbon.php\"><\/script>\n\n\n\n<p>There again by using <code>Carbon<\/code> we largely limit who or what can potentially quickly interact with our API. Instead a way forward would be to do the following:<\/p>\n\n\n<script src=\"https:\/\/gist.github.com\/af49f73749f2a61e7618d844d8f4f1f3.js?file=interface-query-carbon.php\"><\/script>\n\n\n\n<p>The code has not changed but by using the <code>CarbonInterface<\/code> interface, a child interface of <code>DateTimeInterface<\/code> we allow any <code>Carbon<\/code> instance mutable or not to use the method.<\/p>\n\n\n\n<p>But wait, since the code is not even changing or doing any funny stuff with date what we should really do is the following:<\/p>\n\n\n<script src=\"https:\/\/gist.github.com\/af49f73749f2a61e7618d844d8f4f1f3.js?file=interface-query-date.php\"><\/script>\n\n\n\n<p>The following code will work everywhere regardless of the presence of the <code>Carbon<\/code> package.<\/p>\n\n\n\n<p><strong>TL;DR:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span style=\"color: initial; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, sans-serif;\">is Type-hinting fun: &#x2705;<\/span><\/li><li>should we still use <code>Carbon<\/code> (the instance) and <code>DateTime<\/code>: &#x274c;<\/li><li>should we use <code>CarbonImmutable<\/code> and <code>DateTimeImmutable<\/code> : &#x2705;<\/li><li><strong>Rely on contracts and avoid specific implementations or hard coupling when possible<\/strong><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Simple rules to improve type hint in your codebase.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[771,838,837,412,839],"class_list":["post-2956","post","type-post","status-publish","format-standard","hentry","category-web","tag-datetime","tag-decoupling","tag-interface","tag-php","tag-type-hint"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/comments?post=2956"}],"version-history":[{"count":2,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2956\/revisions"}],"predecessor-version":[{"id":2958,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2956\/revisions\/2958"}],"wp:attachment":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/media?parent=2956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/categories?post=2956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/tags?post=2956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}