Mmoreram\PHPFormatter\Fixer\HeaderFixer::fix PHP Method

fix() public method

Fix any piece of code given as parameter.
public fix ( string $data ) : string
$data string Data
return string Data fixed
    public function fix($data)
    {
        $regex = '~(?P<group>^\\s*<\\?php(?:(?:(?:/\\*.*?\\*/)|(?:(?://|#).*?\\n{1})|(?:\\s*))*))(?P<other>.*)~s';
        preg_match($regex, $data, $results);
        if (!isset($results['group'])) {
            return false;
        }
        $other = $results['other'];
        return $this->header . ltrim($other);
    }

Usage Example

 /**
  * Test fixer.
  */
 public function testFix()
 {
     $data = "<?php\n\nnamespace 'App';";
     $header = '';
     $fixedDataExpected = "<?php\n\nnamespace 'App';";
     $headerFixer = new HeaderFixer($header);
     $fixedData = $headerFixer->fix($data);
     $fixedData = $headerFixer->fix($fixedData);
     $this->assertEquals($fixedDataExpected, $fixedData);
 }
All Usage Examples Of Mmoreram\PHPFormatter\Fixer\HeaderFixer::fix