<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <title>EnigmaCurry</title>
    <link>http://www.enigmacurry.com/</link>
    <description>The Curry Enigma</description>
    <pubDate>Thu, 07 Feb 2013 01:43:41 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>PHP Code Compliance In Emacs</title>
      <link>http://www.enigmacurry.com/2011/07/01/php-code-compliance-in-emacs</link>
      <pubDate>Fri, 01 Jul 2011 09:00:00 EDT</pubDate>
      <category><![CDATA[php]]></category>
      <category><![CDATA[emacs]]></category>
      <guid>S3pGCFLQEPa-eWEQfIcHONGFTCQ=</guid>
      <description>PHP Code Compliance In Emacs</description>
      <content:encoded><![CDATA[<p>My job has me working on a project in PHP right now -- it sure isn't
Python, but PHP has grown up considerably since the last time I used
it, which has been <em>awhile</em>. Consequently, I have no Emacs config for
PHP setup, other than vanilla php-mode, so I went in search of
one. Of course, I knew <a href="http://sachachua.com/blog/2008/07/emacs-and-php-on-the-fly-syntax-checking-with-flymake/">Sacha Chua would have a great
one</a>, so
that's where I've started.</p>
<p>My code is required to conform to the <a href="http://pear.php.net/manual/en/standards.php">PEAR coding
standards</a> which is a bit
pedantic in places, but it's generally a good reference to make clean
and readable code. The only real pain in conforming to a coding
standard is if your editor doesn't pick up on your mistakes right
away, you get no automatic feedback and you're left with the task of
cleaning up your code later at an inconvenient time. In my case, a
subversion pre-commit hook checks for compliance and prevents me from
checking in non-conforming code just when I thought I was ready to go
home!</p>
<p>Sacha's configuration only checks for general syntax errors, it
doesn't check for code compliance. For that, I'm using
<a href="http://pear.php.net/package/PHP_CodeSniffer/redirected">PHP_CodeSniffer</a>
which performs a static analysis of a PHP file and notes any
deviations for a given standard. They even <a href="http://pear.php.net/manual/en/package.php.php-codesniffer.reporting.php">support Emacs compile
mode</a>
out of the box. Compile mode still doesn't give me automatic feedback
though, for that I still wanted to use flymake like Sacha has.</p>
<p>So here's my elisp for configuring Emacs to automatically highlight
both syntax errors and coding standard deviations for PHP:</p>
<div class="pygments_murphy"><pre><span class="p">(</span><span class="nb">require</span> <span class="ss">&#39;php-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nb">require</span> <span class="ss">&#39;flymake</span><span class="p">)</span>

<span class="c1">;; Pear coding standards : http://pear.php.net/manual/en/standards.indenting.php</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">pear/php-mode-init</span> <span class="p">()</span>
  <span class="s">&quot;Set some buffer-local variables.&quot;</span>
  <span class="p">(</span><span class="k">setq</span> <span class="nv">case-fold-search</span> <span class="no">t</span><span class="p">)</span>
  <span class="p">(</span><span class="k">setq</span> <span class="nv">indent-tabs-mode</span> <span class="no">nil</span><span class="p">)</span>
  <span class="p">(</span><span class="k">setq</span> <span class="nv">fill-column</span> <span class="mi">78</span><span class="p">)</span>
  <span class="p">(</span><span class="k">setq</span> <span class="nv">c-basic-offset</span> <span class="mi">4</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">c-set-offset</span> <span class="ss">&#39;arglist-cont</span> <span class="mi">0</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">c-set-offset</span> <span class="ss">&#39;arglist-intro</span> <span class="ss">&#39;+</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">c-set-offset</span> <span class="ss">&#39;case-label</span> <span class="mi">2</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">c-set-offset</span> <span class="ss">&#39;arglist-close</span> <span class="mi">0</span><span class="p">))</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">&#39;php-mode-hook</span> <span class="ss">&#39;pear/php-mode-init</span><span class="p">)</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">my-php-hook-function</span> <span class="p">()</span>
  <span class="p">(</span><span class="nb">set</span> <span class="p">(</span><span class="nv">make-local-variable</span> <span class="ss">&#39;compile-command</span><span class="p">)</span> <span class="p">(</span><span class="nb">format</span> <span class="s">&quot;php_lint %s&quot;</span> <span class="p">(</span><span class="nv">buffer-file-name</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">&#39;php-mode-hook</span> <span class="ss">&#39;my-php-hook-function</span><span class="p">)</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">flymake-php-init</span> <span class="p">()</span>
  <span class="s">&quot;Use php and phpcs to check the syntax and code compliance of the current file.&quot;</span>
  <span class="p">(</span><span class="k">let*</span> <span class="p">((</span><span class="nv">temp</span> <span class="p">(</span><span class="nv">flymake-init-create-temp-buffer-copy</span> <span class="ss">&#39;flymake-create-temp-inplace</span><span class="p">))</span>
     <span class="p">(</span><span class="nv">local</span> <span class="p">(</span><span class="nv">file-relative-name</span> <span class="nv">temp</span> <span class="p">(</span><span class="nv">file-name-directory</span> <span class="nv">buffer-file-name</span><span class="p">))))</span>
    <span class="p">(</span><span class="nb">list</span> <span class="s">&quot;php_lint&quot;</span> <span class="p">(</span><span class="nb">list</span> <span class="nv">local</span><span class="p">))))</span>

<span class="c1">;;This is the error format for : php -f somefile.php -l </span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">&#39;flymake-err-line-patterns</span>
  <span class="o">&#39;</span><span class="p">(</span><span class="s">&quot;\\(Parse\\|Fatal\\) error: +\\(.*?\\) in \\(.*?\\) on line \\([0-9]+\\)$&quot;</span> <span class="mi">3</span> <span class="mi">4</span> <span class="no">nil</span> <span class="mi">2</span><span class="p">))</span>

<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">&#39;flymake-allowed-file-name-masks</span> <span class="o">&#39;</span><span class="p">(</span><span class="s">&quot;\\.php$&quot;</span> <span class="nv">flymake-php-init</span><span class="p">))</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">&#39;php-mode-hook</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="p">(</span><span class="nv">flymake-mode</span> <span class="mi">1</span><span class="p">)))</span>
</pre></div>

<p>This depends on a small BASH helper script which I have stored in my
$HOME/bin directory (which is on my PATH by default):</p>
<div class="pygments_murphy"><pre>!/bin/bash
<span class="c">#This does standard PHP syntax checking</span>
php -f <span class="nv">$1</span> -l
<span class="c">#This does coding standard checking</span>
phpcs --standard<span class="o">=</span>PEAR --report<span class="o">=</span>emacs <span class="nv">$1</span>
<span class="c">#Always exit with status code 0 otherwise flymake complains</span>
<span class="nb">exit </span>0
</pre></div>

<p>Now I get nice red highlighting for when I forget a doc tag or to put a space
between parens and brackets. Isn't pedantry great? :/ </p>
<p><center><img src="/blog-post-images/PHP Code Compliance.png"></center></p>]]></content:encoded>
    </item>
  </channel>
</rss>
