Thanks Arch Linux for the kick to learn Docker

Thanks to Arch Linux and its rolling release strategy it comes with PHP 7.1 as default. After some months of having the following in /etc/pacman.conf

IgnorePkg = php php-apache php-gd php-intl php-mcrypt php-pgsql php-geoip php-cgi php-embed php-enchant php-fpm php-imap php-odbc php-phpdbg php-pspell php-snmp php-sqlite php-tidy php-xsl

I pushed myself to learn more and finally start creating some Dockerfiles for some legacy PHP projects.

ss_2017-09-05-11-10-31
Docker host vs. local host PHP
Advertisements

Livereload WordPress PHP code during development

I got tired of reloading 2 browser windows (one incognito) during development. I saw one fellow developer using Livereload in his code so I gave it a try.

Boy was that a good/effective idea.

  1. Create package.json file with npm init
    in your project dir.
  2. Add line "livereload": "^0.6.2" to devDependencies and "dev": "livereload -d wp-content", to scripts
    {
      "name": "example.com",
      "version": "1.0.0",
      "description": "",
      "dependencies": {
      },
      "devDependencies": {
        "livereload": "^0.6.2"
      },
      "scripts": {
        "dev": "livereload -d wp-content",
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "repository": {
        "type": "git",
        "url": "git+ssh://git@bitbucket.org/exampleUser/example.git"
      },
      "author": "Michal Zuber",
      "license": "MIT"
    }
    
  3. Install livereload package npm install
  4. Next add LiveReload Chrome extension
  5. Run npm run dev or yarn dev
  6. Open project URL in browser and click LiveReload extension icon to connect with livereload. Example console output when handshake was successful
    yarn dev v0.28.4
    $ livereload -d wp-content
    LiveReload is waiting for browser to connect.
    
    Protocol version: 7
    Exclusions: /\.git\//,/\.svn\//,/\.hg\//
    Extensions: html,css,js,png,gif,jpg,php,php5,py,rb,erb,coffee
    Polling: false
    
    
    Starting LiveReload v0.6.2 for /srv/http/example.com/-d on port 35729.
    Watching /srv/http/example.com/-d...
    
    Browser connected.
    
    Client message: {"command":"hello","protocols":["http://livereload.com/protocols/official-6","http://livereload.com/protocols/official-7"],"ver":"2.2.2","ext":"Chrome","extver":"2.1.0"}
    
    Client requested handshake...
    
    Handshaking with client using protocol 7...
    
    Client message: {"command":"info","plugins":{"less":{"disable":false,"version":"1.0"}},"url":"http://localhost/example.com/test-page/"}
    

Disable PHPCS rule of long condition

I don’t want to comment long code statements end, but PHP CodeSniffer with WordPress Coding Standards does complain with the following error:

End comment for long condition not found; expected “//end if”

So I added the following to the projects phpcs.xml

Sniff source code PHP_CodeSniffer/src/Standards/Squiz/Sniffs/Commenting/LongConditionClosingCommentSniff.php
For more help with PHPCS rules check out Listing phpcs rules and excluding commenting sniff

Listing phpcs rules and excluding commenting sniff

While testing WordPress-Coding-Standards PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions the following command came handy. It lists all enabled rules:

phpcs --standard=phpcs.xml -e

Output example

The WordPress Project standard contains 100 sniffs

Generic (34 sniffs)
-------------------
  Generic.Classes.DuplicateClassName
  Generic.Classes.OpeningBraceSameLine
  Generic.CodeAnalysis.EmptyStatement
  Generic.CodeAnalysis.ForLoopShouldBeWhileLoop
  Generic.CodeAnalysis.ForLoopWithTestFunctionCall
  Generic.CodeAnalysis.JumbledIncrementer
  Generic.CodeAnalysis.UnconditionalIfStatement
  Generic.CodeAnalysis.UnnecessaryFinalModifier
  Generic.CodeAnalysis.UnusedFunctionParameter
  Generic.CodeAnalysis.UselessOverridingMethod
  Generic.Commenting.DocComment
  Generic.Commenting.Fixme
  Generic.Commenting.Todo
  Generic.ControlStructures.InlineControlStructure
  Generic.Files.ByteOrderMark
  Generic.Files.EndFileNewline
  Generic.Files.LineEndings
  Generic.Formatting.DisallowMultipleStatements
  Generic.Formatting.SpaceAfterCast
  Generic.Functions.CallTimePassByReference
  Generic.Functions.OpeningFunctionBraceKernighanRitchie
  Generic.NamingConventions.UpperCaseConstantName
  Generic.PHP.BacktickOperator
  Generic.PHP.DeprecatedFunctions
  Generic.PHP.DisallowAlternativePHPTags
  Generic.PHP.DisallowShortOpenTag
  Generic.PHP.ForbiddenFunctions
  Generic.PHP.LowerCaseConstant
  Generic.PHP.LowerCaseKeyword
  Generic.PHP.NoSilencedErrors
  Generic.PHP.Syntax
  Generic.Strings.UnnecessaryStringConcat
  Generic.WhiteSpace.DisallowSpaceIndent
  Generic.WhiteSpace.ScopeIndent

PEAR (2 sniffs)
---------------
  PEAR.Functions.FunctionCallSignature
  PEAR.NamingConventions.ValidClassName

PSR2 (3 sniffs)
---------------
  PSR2.ControlStructures.ElseIfDeclaration
  PSR2.ControlStructures.SwitchDeclaration
  PSR2.Files.ClosingTag

Squiz (26 sniffs)
-----------------
  Squiz.Classes.SelfMemberReference
  Squiz.Commenting.BlockComment
  Squiz.Commenting.ClassComment
  Squiz.Commenting.ClosingDeclarationComment
  Squiz.Commenting.DocCommentAlignment
  Squiz.Commenting.EmptyCatchComment
  Squiz.Commenting.FileComment
  Squiz.Commenting.FunctionComment
  Squiz.Commenting.FunctionCommentThrowTag
  Squiz.Commenting.InlineComment
  Squiz.Commenting.VariableComment
  Squiz.ControlStructures.ControlSignature
  Squiz.Functions.FunctionDeclarationArgumentSpacing
  Squiz.Functions.FunctionDuplicateArgument
  Squiz.Operators.IncrementDecrementUsage
  Squiz.Operators.ValidLogicalOperators
  Squiz.PHP.CommentedOutCode
  Squiz.PHP.DisallowMultipleAssignments
  Squiz.PHP.DisallowSizeFunctionsInLoops
  Squiz.PHP.EmbeddedPhp
  Squiz.PHP.Eval
  Squiz.PHP.NonExecutableCode
  Squiz.Strings.ConcatenationSpacing
  Squiz.Strings.DoubleQuoteUsage
  Squiz.WhiteSpace.CastSpacing
  Squiz.WhiteSpace.SuperfluousWhitespace

WordPress (35 sniffs)
---------------------
  WordPress.Arrays.ArrayDeclarationSpacing
  WordPress.Arrays.ArrayIndentation
  WordPress.Arrays.ArrayKeySpacingRestrictions
  WordPress.Arrays.CommaAfterArrayItem
  WordPress.CSRF.NonceVerification
  WordPress.Classes.ClassInstantiation
  WordPress.CodeAnalysis.EmptyStatement
  WordPress.DB.RestrictedClasses
  WordPress.DB.RestrictedFunctions
  WordPress.Files.FileName
  WordPress.Functions.DontExtract
  WordPress.Functions.FunctionCallSignatureNoParams
  WordPress.NamingConventions.PrefixAllGlobals
  WordPress.NamingConventions.ValidFunctionName
  WordPress.NamingConventions.ValidHookName
  WordPress.NamingConventions.ValidVariableName
  WordPress.PHP.DevelopmentFunctions
  WordPress.PHP.DiscouragedPHPFunctions
  WordPress.PHP.POSIXFunctions
  WordPress.PHP.StrictComparisons
  WordPress.PHP.StrictInArray
  WordPress.PHP.YodaConditions
  WordPress.Variables.GlobalVariables
  WordPress.WP.AlternativeFunctions
  WordPress.WP.CapitalPDangit
  WordPress.WP.DeprecatedClasses
  WordPress.WP.DeprecatedFunctions
  WordPress.WP.DeprecatedParameters
  WordPress.WP.DiscouragedFunctions
  WordPress.WP.EnqueuedResources
  WordPress.WP.I18n
  WordPress.WP.PreparedSQL
  WordPress.WhiteSpace.CastStructureSpacing
  WordPress.WhiteSpace.DisallowInlineTabs
  WordPress.WhiteSpace.OperatorSpacing

Now I can disable that annoys me 🙂

 

On demand Xdebug

Xdebug with default_enable=1 makes PHP execution kinda slow. Fortunately it offers on demand profiling when XDEBUG_PROFILE parameter is part of the request.
Make sure you have the following in your php.ini under [xdebug] section:

[xdebug]
xdebug.profiler_enable_trigger=1
xdebug.profiler_enable=0

Resources
https://stackoverflow.com/questions/8077993/can-i-manually-say-on-xdebug-profiler-to-start-profiling-in-specific-place
https://xdebug.org/docs/all_settings#profiler_enable_trigger

Locating used PHP configuration settings

Quick tip to get used PHP configurations.
Run the following in your fav terminal to list paths to the config files:

php -i |grep '\.ini'

Sample output:

Configuration File (php.ini) Path => /usr/local/php5/lib
Loaded Configuration File => /usr/local/php5/lib/php.ini
Scan this dir for additional .ini files => /usr/local/php5/php.d
Additional .ini files parsed => /usr/local/php5/php.d/10-extension_dir.ini,
/usr/local/php5/php.d/20-extension-opcache.ini,
/usr/local/php5/php.d/40-openssl.ini,
/usr/local/php5/php.d/50-extension-apcu.ini,
/usr/local/php5/php.d/50-extension-curl.ini,
/usr/local/php5/php.d/50-extension-gmp.ini,
/usr/local/php5/php.d/50-extension-igbinary.ini,
/usr/local/php5/php.d/50-extension-imap.ini,
/usr/local/php5/php.d/50-extension-intl.ini,
/usr/local/php5/php.d/50-extension-mcrypt.ini,
/usr/local/php5/php.d/50-extension-memcache.ini,
/usr/local/php5/php.d/50-extension-memcached.ini,
/usr/local/php5/php.d/50-extension-mongo.ini,
/usr/local/php5/php.d/50-extension-mssql.ini,
/usr/local/php5/php.d/50-extension-oauth.ini,
/usr/local/php5/php.d/50-extension-pdo_dblib.ini,
/usr/local/php5/php.d/50-extension-pdo_pgsql.ini,
/usr/local/php5/php.d/50-extension-pgsql.ini,
/usr/local/php5/php.d/50-extension-propro.ini,
/usr/local/php5/php.d/50-extension-raphf.ini,
/usr/local/php5/php.d/50-extension-readline.ini,
/usr/local/php5/php.d/50-extension-redis.ini,
/usr/local/php5/php.d/50-extension-solr.ini,
/usr/local/php5/php.d/50-extension-ssh2.ini,
/usr/local/php5/php.d/50-extension-twig.ini,
/usr/local/php5/php.d/50-extension-uploadprogress.ini,
/usr/local/php5/php.d/50-extension-xdebug.ini,
/usr/local/php5/php.d/50-extension-xhprof.ini,
/usr/local/php5/php.d/50-extension-xsl.ini,
/usr/local/php5/php.d/60-extension-pecl_http.ini,
/usr/local/php5/php.d/99-liip-developer.ini