Wednesday, 19 August 2015

phpcs problem with utf-8 encoding - mbstring.func_overload

I was strugling with utf8 files when I wanted to check them with phpcs. Even when the file was ok (checked by removing utf-8 characters) phpcs reported errors and warnings. I assumed that the problem was because of badly parsed file.

I started with fixing phpcs configuration - Setting default encoding for phpcs
phpcs --config-set encoding utf-8

Then I verified that the config is set properly
> cat composer/vendor/squizlabs/php_codesniffer/CodeSniffer.conf
<?php
 $phpCodeSnifferConfig = array (
  'encoding' => 'utf-8',
)
?>

Checked the file and again errors...
> phpcs --standard=MyStandard path/to/file/MySuperObject.php

FILE: ...path/to/file/MySuperObject.php
----------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------
 40 | ERROR | Doc comment for parameter "$cookies" missing
 40 | ERROR | Doc comment for parameter "$cakes" missing
 49 | ERROR | Missing @return tag in function comment
----------------------------------------------------------------------


Also phpcs command with encoding specified did not helped
phpcs --standard=MyStandard --encoding=utf-8 path/to/file/MySuperObject.php

I started to be suspicious about php encoding.
When I found that in my php.ini file the 'default_encoding' is not set I was happy that it must be it.
I set the encoding as UTF-8 and ... same, again same errors.

Then I got an idea - it must be some php configuration which does something with encoding and voala, I spotted this configuration in my php.in

mbstring.func_overload = 6 ; All string functions to mbstring functions

When I deleted this configuration from my php.ini file, phpcs started to work correctly.

Unfortunately I need that ugly configuration for  my application. So now I have to figure out how to left it in php.ini and get rid of it for phpcs.

As I was not successfull with changing that php.ini setting for phpcs maybe the best option is to get rid of mbstring.func_overload.