Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Parser
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 parse
n/a
0 / 0
n/a
0 / 0
0
 parseFromFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 format
n/a
0 / 0
n/a
0 / 0
0
 formatToFile
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 newChangelogEntry
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 newChangeEntry
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Base class for a changelog parser.
4 *
5 * @package automattic/jetpack-changelogger
6 */
7
8namespace Automattic\Jetpack\Changelog;
9
10/**
11 * Base class for a changelog parser.
12 */
13abstract class Parser {
14
15    /**
16     * Parse changelog data into a Changelog object.
17     *
18     * @param string $changelog Changelog contents.
19     * @return Changelog
20     */
21    abstract public function parse( $changelog ); // @codeCoverageIgnore
22
23    /**
24     * Parse changelog data from a file or stream into a Changelog object.
25     *
26     * @param string|resource $file Filename or stream resource.
27     * @return Changelog
28     */
29    public function parseFromFile( $file ) {
30        return $this->parse( is_string( $file ) ? file_get_contents( $file ) : stream_get_contents( $file ) );
31    }
32
33    /**
34     * Write a Changelog object to a string.
35     *
36     * Note that, while an implementation should try to handle any Changelog
37     * object appropriately, data loss may occur if the Changelog was not
38     * generated by an instance of the same Parser with the same configuration.
39     *
40     * @param Changelog $changelog Changelog object.
41     * @return string
42     */
43    abstract public function format( Changelog $changelog ); // @codeCoverageIgnore
44
45    /**
46     * Write a Changelog object to a file or stream.
47     *
48     * @param string|resource $file Filename or stream resource.
49     * @param Changelog       $changelog Changelog object.
50     * @return bool Whether the write succeeded.
51     */
52    public function formatToFile( $file, Changelog $changelog ) {
53        $contents = $this->format( $changelog );
54
55        if ( is_string( $file ) ) {
56            return file_put_contents( $file, $contents ) === strlen( $contents );
57        } else {
58            while ( '' !== $contents ) {
59                $l = fwrite( $file, $contents );
60                if ( ! $l ) { // Contrary to the docs, it sometimes returns 0 on error too.
61                    return false;
62                }
63                $contents = (string) substr( $contents, $l );
64            }
65            return true;
66        }
67    }
68
69    /**
70     * Create a new ChangelogEntry.
71     *
72     * @param string $version See `ChangelogEntry::__construct()`.
73     * @param array  $data See `ChangelogEntry::__construct()`.
74     * @return ChangelogEntry
75     */
76    public function newChangelogEntry( $version, $data = array() ) {
77        return new ChangelogEntry( $version, $data );
78    }
79
80    /**
81     * Create a new ChangeEntry.
82     *
83     * @param array $data See `ChangeEntry::__construct()`.
84     * @return ChangeEntry
85     */
86    public function newChangeEntry( $data = array() ) {
87        return new ChangeEntry( $data );
88    }
89}