Mmoreram\PHPFormatter\Fixer\StrictFixer::fix PHP 메소드

fix() 공개 메소드

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

Usage Example

예제 #1
0
    /**
     * Test fixer.
     *
     * @dataProvider dataFix
     */
    public function testFix($data, $withHeader, $strict)
    {
        $fixedDataExpected = '<?php' . ($withHeader ? '

/**
 * Header
 */' : '') . '

' . (is_bool($strict) ? 'declare(strict_types=' . ($strict ? '1' : '0') . ');

' : '') . "namespace 'namespace';";
        $strictFixer = new StrictFixer($strict);
        $fixedData = $strictFixer->fix($data);
        $this->assertEquals($fixedDataExpected, $fixedData);
    }
All Usage Examples Of Mmoreram\PHPFormatter\Fixer\StrictFixer::fix