Automatically apply PSR-2 coding style

*UPDATE*

In version 2 of php_cs_fixer some parameters and rules are renamed. Upgrade guide can be see on their Github page. This how-to is accordingly updated.


Applying PSR-2 coding style manually is really no fun and wasting time. A short visit in Github and there’s a tool which detects and fixes the coding style. The tool is called php-cs-fixer.

I’ll show you how to get the tool and use it to easily reformat all PHP file in project.

Continue reading Automatically apply PSR-2 coding style

Fail downloading file on Android

How hard is it, to write a PHP script to allow user to download a file? really easy, one would say. Just send the appropriate HTTP header and echo the file content.

1
2
3
4
5
< ?php
  header('Content-type: application/force-download');
  header('Content-Disposition: attachment; filename="test.txt"');
  echo(file_get_content('/path/to/file'));
?>

Basically that’s all I need to force download per PHP. But somehow Android stock browser (Chrome) only downloads and creates a 0 kB file. Other browser (tested with Firefox on Android) saves the file correctly.

What does happen here?

Continue reading Fail downloading file on Android

Standalone view in extbase

Usually a view in extbase is always called from a controller.  but what if, an action in your controller is called as an eID and only parts of the template needs to be rendered? Partial is an option, but I have a code snippet, which uses the

1
TYPO3\\CMS\\Fluid\\View\\StandaloneView

class.

Continue reading Standalone view in extbase

[PHP] Removing duplicate in an array… really fast

Let’s imagine you have a huge array, with say couple ten thousands of elements and you want to remove any duplicates entries in this array. As PHP programmer you’ll be thinking of array_unique. For a “small” sized array and one dimensional array this one does the trick.

I have some nice snippets which cover multi dimensional and/or huge sized array.

First: you have a huge, one dimensional array.

array_flip(array_flip(array_reverse($input,true)))

This one works only on one dimensional array. The key of the last element will be preserved and the keys are not sorted

Second: for a huge, multi dimensional array

function super_unique($array) {
  $result = array_map("unserialize", array_unique(array_map("serialize", $array)));
  foreach ($result as $key =&gt; $value) {
    if ( is_array($value) ) {
      $result[$key] = super_unique($value);
    }
  }
  return $result;
}

sources:

  1. http://dk.php.net/manual/en/function.array-unique.php#97285
  2. http://www.puremango.co.uk/2010/06/fast-php-array_unique-for-removing-duplicates/