1<?php
2/*
3 * This file is part of the php-code-coverage package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace SebastianBergmann\CodeCoverage\Report;
12
13use SebastianBergmann\CodeCoverage\CodeCoverage;
14
15/**
16 * Uses var_export() to write a SebastianBergmann\CodeCoverage\CodeCoverage object to a file.
17 */
18class PHP
19{
20    /**
21     * @param CodeCoverage $coverage
22     * @param string       $target
23     *
24     * @return string
25     */
26    public function process(CodeCoverage $coverage, $target = null)
27    {
28        $filter = $coverage->filter();
29
30        $output = sprintf(
31            '<?php
32$coverage = new SebastianBergmann\CodeCoverage\CodeCoverage;
33$coverage->setData(%s);
34$coverage->setTests(%s);
35
36$filter = $coverage->filter();
37$filter->setWhitelistedFiles(%s);
38
39return $coverage;',
40            var_export($coverage->getData(true), 1),
41            var_export($coverage->getTests(), 1),
42            var_export($filter->getWhitelistedFiles(), 1)
43        );
44
45        if ($target !== null) {
46            return file_put_contents($target, $output);
47        } else {
48            return $output;
49        }
50    }
51}
52