<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alexander Terehov on Medium]]></title>
        <description><![CDATA[Stories by Alexander Terehov on Medium]]></description>
        <link>https://medium.com/@terales?source=rss-71a9ca081abb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RGej5g1ZaRhBOzMz0oDw9Q.jpeg</url>
            <title>Stories by Alexander Terehov on Medium</title>
            <link>https://medium.com/@terales?source=rss-71a9ca081abb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 12:38:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@terales/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How collapsed code helped me to merge broken code into master]]></title>
            <link>https://medium.com/terales-engineering/how-collapsed-code-helped-me-to-merge-broken-code-into-master-7205761061d7?source=rss-71a9ca081abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/7205761061d7</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[pull-request]]></category>
            <dc:creator><![CDATA[Alexander Terehov]]></dc:creator>
            <pubDate>Thu, 22 Aug 2019 09:20:00 GMT</pubDate>
            <atom:updated>2019-08-22T09:23:27.184Z</atom:updated>
            <content:encoded><![CDATA[<p>Today I’ve spent a couple of hours on learning a lesson about best practice when working with pull requests in GitHub.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*HeKXVkKaiSJhiy-z81v4UQ.png" /><figcaption>Hotfixes in new PRs were using the same feature branch</figcaption></figure><p>We have a feature merged on master and deployed, but in several days there was a bug discovered.</p><p>Rafael, the initial developer of the feature, fixed it in pull-request #3405:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*XqK9oEAOFUJCs0yE3SVNHw.png" /></figure><p>I’ve reviewed and tested it and it was working fine for me as an admin of the website, so I’ve merged it without any hesitation.</p><p>It turned out the bug was still happening for users! And the reason was here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/1*cwhzv7KzwTkzSz2W3xJ8-A.png" /><figcaption>the `else` statement was in an outside block</figcaption></figure><p>It was my mistake as a reviewer to assume that else is properly placed.</p><h3>Lesson learned: look inside the collapsed code</h3><p>at least, if there is a piece which was added to an existing block.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*vMpYOsmbNEVrU10vJ22wbA.gif" /></figure><p>Also, this bug wasn’t apparent during mine manual testing because PhPStorm somehow didn’t upload the file to the remote development server as it configured to do AND I’ve missed the error message.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7205761061d7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/terales-engineering/how-collapsed-code-helped-me-to-merge-broken-code-into-master-7205761061d7">How collapsed code helped me to merge broken code into master</a> was originally published in <a href="https://medium.com/terales-engineering">terales engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Validate URL with UTF-8 symbols in PHP]]></title>
            <link>https://medium.com/terales-engineering/validate-url-with-utf-8-symbols-in-php-e3b0434c96bf?source=rss-71a9ca081abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3b0434c96bf</guid>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Alexander Terehov]]></dc:creator>
            <pubDate>Fri, 09 Nov 2018 16:57:36 GMT</pubDate>
            <atom:updated>2018-11-09T16:57:36.151Z</atom:updated>
            <content:encoded><![CDATA[<h3>Validate URL in PHP without regex</h3><p>In PHP we have had a default URL validation like this:</p><pre>// truthy if we have valid URL<br>filter_var($url, FILTER_VALIDATE_URL);</pre><p>But this approach would fail for working URLs with utf-8 symbols. Consider this website: <a href="http://www.xn--medizinische-bersetzungen-rwc.biz/">http://www.medizinische-übersetzungen.biz/</a></p><p>Validation of the example from above will fail because `filter_var` implementation is not aware of multibyte encodings.</p><p>A safer approach to validate URL you’re receiving from users:</p><pre>// truthy if we have valid URL<br>parse_url(https://medium.com/$url,%20PHP_URL_SCHEME) &amp;&amp; parse_url(https://medium.com/$url,%20PHP_URL_HOST);</pre><p>There is a drawback in the suggested solution: parse_urlwon’t catch strange characters in URL:</p><pre>input: <a href="http://www.ex=ample.com/">http://www.ex=ample.com</a><br>filter_var: FAIL<br>parse_url: PASS  // the only failure of suggested solution</pre><p>But from experience, I usually see a missing schema or completely invalid values in the form field, this solution is good enough.</p><p>Check test cases in <a href="https://github.com/terales/php-url-validation-example">https://github.com/terales/php-url-validation-example</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B8Le25xXLgE_YLwhwVMJHQ.png" /><figcaption>image for preview only</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3b0434c96bf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/terales-engineering/validate-url-with-utf-8-symbols-in-php-e3b0434c96bf">Validate URL with UTF-8 symbols in PHP</a> was originally published in <a href="https://medium.com/terales-engineering">terales engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Starting Apache and MySQL on project startup in PHPStorm [Windows]]]></title>
            <link>https://medium.com/terales-engineering/starting-apache-and-mysql-on-project-open-windows-f95862b077db?source=rss-71a9ca081abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/f95862b077db</guid>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[phpstorm]]></category>
            <category><![CDATA[development-tips]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[automation]]></category>
            <dc:creator><![CDATA[Alexander Terehov]]></dc:creator>
            <pubDate>Tue, 31 Oct 2017 23:33:36 GMT</pubDate>
            <atom:updated>2017-11-05T15:13:24.588Z</atom:updated>
            <content:encoded><![CDATA[<p>Do you want to automatically start some special services in a project when you open PHPStorm? You can!</p><h3>Set services to a manual start</h3><p>Don’t close Services as you may use it later for copy/paste exact values into command line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*BaLWdXu0iN6aXJuKeHnv3g.png" /><figcaption>Set startup type to “Manual”, you don’t want services to slow you down :)</figcaption></figure><h3>Start service from your command line</h3><p>By default you can’t start or stop service from command line if it didn’t started as administrator. To overcome this you need a SubInACL tool as <a href="https://stackoverflow.com/a/18856461/1363799">suggested in this StackOverflow answer</a>.</p><p><a href="https://www.microsoft.com/en-us/download/confirmation.aspx?id=23510">Download SubInACL from Microsoft website</a> and run in command line:</p><pre>cd C:\Program Files (x86)\Windows Resource Kits\Tools\<br>subinacl.exe /service MySQL57 /grant=COMPUTER_NAME\user=PTO</pre><p><strong>MySQL57:</strong> Service name should be exact one, check it in the Services → Properties → Service Name.</p><p><strong>COMPUTER_NAME\user</strong>: this one could be hard to type exactly, so go to Services → Properties → Log On → Browse. Enter part of your user name, hit Check Names and you’ll get full name you need to use in the script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*XJGXr4M1G0nkPL0XiJ8lig.png" /><figcaption>Get your user name as it needed by SubInACL</figcaption></figure><p><strong>PTO</strong>: this stands for P (Pause/Continue Service), T (Start Service) and O (Stop Service). You can check <a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;288129">full list of rights at Microsoft Support Site</a>.</p><h3>Configure Startup tasks in Project settings at PHPStorm</h3><p>Go to Settings → Tools → Startup Tasks. Hit + and select “PHP Web Application”. I use this because it make it possible to run this configuration without any special files with URL only.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*T1w1LeVbGx7Bx-GVEk1Mcw.png" /><figcaption>My project startup configuration</figcaption></figure><p>Click on + under Before launch section and add External tools, like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RvPm_RubBd8UGJ3HxIvmyQ.png" /><figcaption>Start MySQL service on project startup</figcaption></figure><p>Remember to uncheck all boxes to hide this task from other places and to prevent from poping up in console. All we want is to run silently, so it would just work without additional clicks.</p><h3>Try it!</h3><p>You’ll love this magic especially on the projects which you touching on rare occasions and often forget start up details.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f95862b077db" width="1" height="1" alt=""><hr><p><a href="https://medium.com/terales-engineering/starting-apache-and-mysql-on-project-open-windows-f95862b077db">Starting Apache and MySQL on project startup in PHPStorm [Windows]</a> was originally published in <a href="https://medium.com/terales-engineering">terales engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[XDebug on a shared development server]]></title>
            <link>https://medium.com/terales-engineering/xdebug-on-a-shared-development-server-4bbebf993eb4?source=rss-71a9ca081abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/4bbebf993eb4</guid>
            <category><![CDATA[debugging]]></category>
            <dc:creator><![CDATA[Alexander Terehov]]></dc:creator>
            <pubDate>Sat, 28 Oct 2017 23:32:16 GMT</pubDate>
            <atom:updated>2017-10-28T23:34:14.088Z</atom:updated>
            <content:encoded><![CDATA[<p>How to debug inside PHPStorm if:<br>• you have a shared development server,<br>• pages are served via Apache with PHP as module,<br>• XDebug extension is installed and enabled,<br>• you working remotely without VPN?</p><h3>Enable remote debugging in the .htaccess</h3><p>As you don’t want to bother other devs with slower page rendering, you’ll enable remote debugging only for your instance. Add these lines to the .htaccess file:</p><pre><strong>php_flag  </strong>xdebug.remote_enable on<br><strong>php_value </strong>xdebug.remote_host &quot;127.0.0.1&quot;<br><strong>php_value </strong>xdebug.remote_port 9000<br><strong>php_value </strong>xdebug.idekey PHPSTORM<br>php_flag  xdebug.remote_autostart on # you won&#39;t play with cookies</pre><h3>Set up a secure connection between development server and your machine</h3><p>As you working without VPN or without static IP you would like to set up a SSH tunnel. We have good explanation at the <a href="https://confluence.jetbrains.com/display/PhpStorm/Remote+debugging+in+PhpStorm+via+SSH+tunnel#RemotedebugginginPhpStormviaSSHtunnel-2.SetupanSSHtunneltotheremotemachine">JetBrains knowledge base</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*YAEhqyJEVCGeGvPpcq9IVA.png" /><figcaption>Enabling SSH tunel on PuTTY</figcaption></figure><p>Before you debug you would need to establish connection and log into development machine. I could debug while PuTTY is opened and logged only.</p><h3>Configure PHPStorm with path mappings</h3><p>It’s important to configure path mappings in the development server used for Run/Debug Configuration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*iTDsDPGpstqXBOsZowyVkg.png" /><figcaption>XDebug configuration on the PHPStorm side</figcaption></figure><h3>It should work</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*2nFQqFitsKrxw3mfjXy_CA.png" /><figcaption>It works!</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4bbebf993eb4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/terales-engineering/xdebug-on-a-shared-development-server-4bbebf993eb4">XDebug on a shared development server</a> was originally published in <a href="https://medium.com/terales-engineering">terales engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to deprecate properties of the God Object (refactoring the Blob pattern)]]></title>
            <link>https://medium.com/terales-engineering/how-to-deprecate-properties-of-the-god-object-refactoring-the-blob-pattern-b5a1cbccf1fd?source=rss-71a9ca081abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5a1cbccf1fd</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[refactoring]]></category>
            <dc:creator><![CDATA[Alexander Terehov]]></dc:creator>
            <pubDate>Sun, 09 Jul 2017 22:11:29 GMT</pubDate>
            <atom:updated>2017-07-09T22:11:29.124Z</atom:updated>
            <content:encoded><![CDATA[<p>First actionable step to <a href="https://sourcemaking.com/antipatterns/the-blob">clean up a Blob</a> is to remove deprecated properties. Consider object like this:</p><pre>$godObject = <strong>new </strong>stdClass();<br>…<br>$godObject-&gt;db = new db_connection();<br>$godObject-&gt;db_new = new db_connection();<br>…<br>// 140 properties more…<br>…</pre><pre>$godObject-&gt;user = new User();<br>$godObject-&gt;page = new Page();<br>…<br>$godObject-&gt;publicPath = &#39;…/some/path&#39;;</pre><p>Let’s try to remove $godObject-&gt;db property. I can just search for it in the code base and replace all occurrences with $godObject-&gt;db_new. But I’m not any good in manual changing some code though out a large code base, so I want to act in two steps:<br>1. replace all found occurrences,<br>2. set up an alert about deprecated property read/write access, so I can fix any missed dependencies.</p><h3>Listen to property access with __set/__get methods</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d5e108ad2e1fe675d6143826c06816b/href">https://medium.com/media/5d5e108ad2e1fe675d6143826c06816b/href</a></iframe><p><a href="https://github.com/terales/deprecate-properties">Works fine (check yourself)</a>, but there are some concerns about performance degradation of the temporary solution:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*X6GVNGKFq4rfL_TLdLLSNQ.png" /><figcaption>Access dynamic properties via `__get/__set` is roughly 346 times slowen that without them. Screenshot from QCachegrind 0.7.4. Measured in PHP 5.6.30 with XDebug profiler.</figcaption></figure><p>But would it be an issue for a real code?</p><h3>Drop-in replacement stdClass with a custom DeprecateProperties class</h3><p>After adding class inline into the file and switch into the new class I’ve found several errors:</p><pre>Fatal error: Cannot assign by reference to overloaded object in …</pre><p><a href="https://derickrethans.nl/overloaded-properties-get.html">Derick Rethans thinks that passing property by reference didn’t really work</a>:</p><blockquote>Because __get() returns a read-only variable assigning a reference can not work as that requires a variable to be in read/write mode. Most likely the code didn’t work properly in PHP 5.1 either but luckily this is still in an unreleased component. However, I am not aware of a work-around here.</blockquote><p>I’ve removed two occurrences of the assigning be references a run the code on the development environment — there was no any noticeable performance downgrade. Yay!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5a1cbccf1fd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/terales-engineering/how-to-deprecate-properties-of-the-god-object-refactoring-the-blob-pattern-b5a1cbccf1fd">How to deprecate properties of the God Object (refactoring the Blob pattern)</a> was originally published in <a href="https://medium.com/terales-engineering">terales engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Be careful with PhpStorm autosuggesting and automated checks]]></title>
            <link>https://medium.com/terales-engineering/be-careful-with-phpstorm-autosuggesting-and-automated-checks-f8734022f603?source=rss-71a9ca081abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8734022f603</guid>
            <category><![CDATA[phpstorm]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Alexander Terehov]]></dc:creator>
            <pubDate>Mon, 03 Jul 2017 20:06:50 GMT</pubDate>
            <atom:updated>2017-07-03T20:06:50.192Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve shipped a bug to the code base which heavily relies on include_once statements. I thought I’ve tested it, but it ended up to be a case like this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FheRUYSN_As4%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DheRUYSN_As4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FheRUYSN_As4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/5cc37f8fc5941d76b9f284fb51fc04fc/href">https://medium.com/media/5cc37f8fc5941d76b9f284fb51fc04fc/href</a></iframe><p>I thought I’ve messed up with Settings → Editor → Inspections → PHP → Undefined functions, but it was all checked as in default set up.</p><p>php_codesniffer and PHP Mess Detector didn’t show any errors about using undefined function here.</p><p>Found <a href="https://stackoverflow.com/a/41956923/1363799">an explanation at StackOverflow</a>:</p><blockquote>PhpStorm does not take include/require statements into consideration when dealing with definitions — it assumes (for convenience and performance reasons) that <strong>all global entities (like functions/classes etc) are always accessible</strong> <br>(e.g. classes can be auto loaded .. or necessary file with functions <br>is included/required in another place).</blockquote><p>I don’t like to have to remember whether file was included previously or not. So here is a solution.</p><h4>Just run the code and see if it fails</h4><p>We must actually run it manually or with automated tests. As my colleague suggests:</p><blockquote>…we need to always make a point of exercising the changes before deploying them, even if it seems obvious that they should work. For example, temporarily hard-code the parameter you are checking for one time, to test it.</blockquote><p>This is a good lesson for me that we shouldn’t rely too much on static tooling and we should maintain a fast feedback loop which actually runs the code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8734022f603" width="1" height="1" alt=""><hr><p><a href="https://medium.com/terales-engineering/be-careful-with-phpstorm-autosuggesting-and-automated-checks-f8734022f603">Be careful with PhpStorm autosuggesting and automated checks</a> was originally published in <a href="https://medium.com/terales-engineering">terales engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Solving composer’s `Invalid argument` on Windows 10]]></title>
            <link>https://medium.com/terales-engineering/solving-composers-invalid-argument-on-windows-10-df150e642bbf?source=rss-71a9ca081abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/df150e642bbf</guid>
            <category><![CDATA[composer]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[zend-framework]]></category>
            <dc:creator><![CDATA[Alexander Terehov]]></dc:creator>
            <pubDate>Sun, 02 Jul 2017 04:55:09 GMT</pubDate>
            <atom:updated>2017-07-02T04:55:09.324Z</atom:updated>
            <content:encoded><![CDATA[<p>I see this error while jumping into code base which use Zend Framework 1 and running composer install (php 5.6, composer 1.2.2):</p><pre>[ErrorException]<br> ZipArchive::extractTo(C:\Users\░░░░░\Documents\GitHub\░░░░-web-app\░░░░-zf/vendor/composer/242a794b/ZendFramework-1<br> .12.░░\library\Zend\Service\WindowsAzure\CommandLine\Scaffolders\DefaultScaffolder\resources\PhpOnAzure.Web\resourc<br> es\WebPICmdLine/Microsoft.Web.PlatformInstaller.dll): failed to open stream: Invalid argument</pre><p>Found helpful <a href="https://github.com/composer/composer/issues/3755#issuecomment-107927116">comment on the related GitHub issue</a>:</p><blockquote>Apparently composer will sometimes tell you the path length is too long,<br>other times it won’t.</blockquote><p>The most common recommendation is to run<br>composer install --prefer-sourceto omit calling ZipArchiveat all. But I can’t do that because project configuration has pinned zipped source:</p><pre>{<br> &quot;type&quot;: &quot;package&quot;,<br> &quot;package&quot;: {<br>  &quot;name&quot;: &quot;░░░░/zf1&quot;,<br>  &quot;version&quot;: &quot;1.12.░░&quot;,<br>  &quot;dist&quot;: {<br>   &quot;url&quot;: &quot;<a href="https://packages.zendframework.com/releases/ZendFramework-1.12.15/ZendFramework-1.12.15.zip">https://packages.zendframework.com/releases/ZendFramework-1.12.</a>░░<a href="https://packages.zendframework.com/releases/ZendFramework-1.12.15/ZendFramework-1.12.15.zip">/ZendFramework-1.12.</a>░░<a href="https://packages.zendframework.com/releases/ZendFramework-1.12.15/ZendFramework-1.12.15.zip">.zip</a>&quot;,<br>   &quot;type&quot;: &quot;zip&quot;<br>  },<br>  &quot;autoload&quot;: {<br>   &quot;psr-0&quot;: {<br>    &quot;Zend_&quot;: &quot;library/&quot;<br>   }<br>  },<br>  &quot;include-path&quot;: [<br>   &quot;library/&quot;<br>  ],<br>  &quot;config&quot;: {<br>   &quot;bin-dir&quot;: &quot;bin&quot;<br>  }<br> }<br>},</pre><p>If path is too long for Windows, I’ll run unix on top.</p><h4>Git Bash won’t help</h4><p>I’ve tried to run composer install with Git Bash, but it shows the same error. So I can assume that this error is bound with system or php version, or their interaction.</p><h4>Ubuntu via Vargant is going to help</h4><p>I’ve tried a fast workaround with vargrant which worked well. Configuration file inside folder with composer.json:</p><pre># -*- mode: ruby -*-<br># vi: set ft=ruby :</pre><pre>Vagrant.configure(&quot;2&quot;) do |config|<br>  config.vm.box = &quot;ubuntu/xenial64&quot;<br>end</pre><p>Running from Git Bash to have ssh without dealing with Putty keys:</p><pre>vagrant up<br>vagrant ssh<br>sudo php install # installs php7.0<br>cd /vagrant<br>php composer.phar install --ignore-platform-reqs</pre><p>It was crucial to add --ignore-platform-reqs so composer could install dependencies with only default php extensions installed.</p><h3>Simple solution is to use php 7.1</h3><p>The last step is to remove all that installation complexity for a future team members. Updating composer to a current stable 1.4.2 version won’t help.</p><p>But there is <a href="https://github.com/composer/composer/issues/604#issuecomment-267136079">a comment that it’s fixed with php7.1</a>:</p><blockquote>This is no longer an issue with PHP 7.1. 🎉</blockquote><p>And yes! It works without any problems. Record was added into the internal wiki and investigation closed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df150e642bbf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/terales-engineering/solving-composers-invalid-argument-on-windows-10-df150e642bbf">Solving composer’s `Invalid argument` on Windows 10</a> was originally published in <a href="https://medium.com/terales-engineering">terales engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MySQL `on duplicate key update` with unique index and PDO support]]></title>
            <link>https://medium.com/terales-engineering/mysql-on-duplicate-key-update-with-unique-index-and-pdo-support-410ee63b4463?source=rss-71a9ca081abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/410ee63b4463</guid>
            <category><![CDATA[pdo]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Alexander Terehov]]></dc:creator>
            <pubDate>Mon, 19 Jun 2017 03:47:23 GMT</pubDate>
            <atom:updated>2017-06-19T03:47:23.739Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve implemented saving user data from OAuth2 provider and want to update name and image from source every time user logs in. I was surprised to see that on duplicate key update clause works well if we have both primary key and other unique index:</p><pre>INSERT INTO users (`uuid`, `name`, `image_url`)<br>VALUES(:uuid, :name, :image_url)<br>ON DUPLICATE KEY UPDATE<br>   `name` = :name,<br>   `image_url` = :image_url;</pre><pre>-- Assuming that `users` table has these keys<br>ALTER TABLE `users`<br>  ADD PRIMARY KEY (`id`),<br>  ADD UNIQUE KEY `uuid` (`uuid`);</pre><p>I’ve found that from careful reading <a href="https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html">MySQL manual in 13.2.5.3 INSERT … ON DUPLICATE KEY UPDATE Syntax</a>:</p><blockquote>If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause <strong>a duplicate value in a </strong><strong>UNIQUE index </strong>or PRIMARY KEY, MySQL performs an <a href="https://dev.mysql.com/doc/refman/5.7/en/update.html">UPDATE</a> of the old row.</blockquote><h3><strong>Get PDO::lastInsertId() to work with on duplicate key update clause</strong></h3><p>With query like above we can always call same function to get authenticated and updated record id for either new or returned users:</p><pre>$sql = &#39;INSERT INTO users (`uuid`, `name`, `image_url`)<br>        VALUES(:uuid, :name, :image_url)<br>        ON DUPLICATE KEY UPDATE<br>          `site_name` = :site_name,<br>          `image_url` = :image_url&#39;;<br>$statement = $this-&gt;pdo-&gt;prepare($sql)-&gt;execute(…);<br>return $this-&gt;pdo-&gt;lastInsertId();</pre><p>This should work as described in <a href="https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html">MySQL manual in 13.2.5.3 INSERT … ON DUPLICATE KEY UPDATE Syntax</a>:</p><blockquote>If a table contains an AUTO_INCREMENT column and <a href="https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html">INSERT ... ON DUPLICATE KEY UPDATE</a> inserts <strong>or updates</strong> a row, the <a href="https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id">LAST_INSERT_ID()</a> function returns the AUTO_INCREMENT value.</blockquote><p>The problem is that in case of updating record LAST_INSERT_ID() from MySQL will return 0. Why?</p><p>Because PDO calls LAST_INSERT_ID() without arguments. From <a href="https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id">LAST_INSERT_ID() manual</a>:</p><blockquote>With no argument, <a href="https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id">LAST_INSERT_ID()</a> returns a … value representing the first automatically generated <strong>value successfully inserted</strong> for an AUTO_INCREMENT column …</blockquote><p>To workaround we should pass argument to the LAST_INSERT_ID(), so the next call without arguments will receive correct id:</p><blockquote>If <em>expr</em> is given as an argument to <a href="https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id">LAST_INSERT_ID()</a>, the value of the argument is returned by the function and is remembered as the next value to be returned by<a href="https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id">LAST_INSERT_ID()</a></blockquote><p>This means that we can update our primary key with statement like this inside our UPDATE statement:</p><pre>`id` = LAST_INSERT_ID(`id`)<br>                       ^^ -- `id` here is a value of the updated row</pre><h3>The final PHP should look like this:</h3><pre>$sql = &#39;INSERT INTO users (`uuid`, `name`, `image_url`)<br>        VALUES(:uuid, :name, :image_url)<br>        ON DUPLICATE KEY UPDATE<br>          `id` = LAST_INSERT_ID(`id`),     &lt;----- Added to the query<br>          `proz_site_name` = :site_name,<br>          `proz_image_url` = :image_url&#39;;<br>$statement = $this-&gt;pdo-&gt;prepare($sql)-&gt;execute(…);<br>return $this-&gt;pdo-&gt;lastInsertId();</pre><p>Of course, I’ve found <a href="http://php.net/manual/en/pdo.lastinsertid.php#115739">explanation comment on PHP Manual</a> and <a href="https://stackoverflow.com/a/779252/1363799">StackOverflow answer</a> while preparing this post about the issue.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=410ee63b4463" width="1" height="1" alt=""><hr><p><a href="https://medium.com/terales-engineering/mysql-on-duplicate-key-update-with-unique-index-and-pdo-support-410ee63b4463">MySQL `on duplicate key update` with unique index and PDO support</a> was originally published in <a href="https://medium.com/terales-engineering">terales engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>