readme-builder.php ➔ parse_markdown()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 10
nop 1
dl 0
loc 43
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is a readme.txt builder used to publish updates on WordPress repository.
4
 * It avoid duplicating README.md files and ensure simplicity on Github.
5
 *
6
 * @package wp-mautic
7
 */
8
9
// -----------------------------------------------------------------------------
10
/**
11
 * Markdown parser helper
12
 *
13
 * @param  string $file
14
 * @return array
15
 */
16
function parse_markdown( string $file ) :array {
17
	$source = fopen( $file, 'r' );
18
	$content = ['default' => ''];
19
	$current = 'default';
20
	$complement = '';
21
	$level = 0;
22
	$sourceCode = false;
23
	while ( ! feof( $source ) ) {
24
		$line = rtrim(fgets( $source ));
25
		if ( '' === $line ) {
26
			$content[ $current ] .= PHP_EOL;
27
			continue;
28
		}
29
		if ( strpos( $line, '#' ) === 0 ) {
30
			$tmp = strpos( $line, ' ' );
31
			if ( $level > 1 ) {
32
				if( $tmp > $level ) {
33
					$complement = $current . '{}';
34
				} elseif($tmp < $level) {
35
					$exploded = explode('{}', $current);
36
					$complement = count($exploded) <= 2
37
						? ''
38
						: implode('{}', array_slice($exploded, 0, count($exploded)-2)).'{}';
39
40
				}
41
			} else {
42
				$complement = '';
43
			}
44
			$current = $complement . trim( substr( $line, $tmp ) );
45
			$content[ $current ] = '';
46
			$level = $tmp;
47
		} else {
48
			if (0 === strpos($line, '`')) {
49
				$sourceCode = !$sourceCode;
50
				continue;
51
			}
52
53
			$content[ $current ] .= (true === $sourceCode?'    ':'').$line . PHP_EOL;
54
		}
55
	}
56
57
	return $content;
58
}
59
60
// -----------------------------------------------------------------------------
61
// readme.txt template
62
$template = <<<TXT
63
=== WP Mautic ===
64
Author: mautic
65
Tags: marketing, automation
66
%tags%
67
Donate link: http://mautic.org/
68
69
== Description ==
70
71
%heading%
72
73
## Key features
74
%key-features%
75
76
## Configuration
77
78
%configuration%
79
80
## Usage
81
82
%documentation%
83
84
== Installation ==
85
86
%installation%
87
88
== Upgrade Notice ==
89
90
%upgrade%== Changelog ==
91
92
%changelog%
93
TXT;
94
95
// -----------------------------------------------------------------------------
96
// Extract package tag
97
$source = fopen( __DIR__ . '/../wpmautic.php', 'r' );
98
$tags = [];
99
while ( ! feof( $source ) ) {
100
	$line = fgets( $source );
101
	if ( " * @package wp-mautic\n" === $line ) {
102
		break;
103
	}
104
	if (
105
		0 === strpos( $line, ' * ' ) &&
106
		false === strpos( $line, 'Author:' ) &&
107
		false === strpos( $line, 'Plugin ' )
108
	) {
109
		$tags[] = str_replace( 'Version:', 'Stable tag:', trim( substr( $line, 3 ) ) );
110
	}
111
}
112
$tags = implode( PHP_EOL, $tags );
113
114
// -----------------------------------------------------------------------------
115
// Extract upgrade notices
116
$notices = [];
117
foreach ( parse_markdown( __DIR__ . '/../UPGRADE.md' ) as $key => $notice ) {
118
	if ( ! preg_match( '/^v.*/', $key ) ) {
119
		continue;
120
	}
121
122
	$notices[] = "= $key =\n$notice";
123
}
124
$notices = implode( $notices );
125
126
// -----------------------------------------------------------------------------
127
// Extract changelog
128
$changelog = '';
129
$title = '';
130
foreach ( parse_markdown( __DIR__ . '/../CHANGELOG.md' ) as $key => $notice ) {
131
	if ( ! preg_match( '/^\[v.*/', $key ) ) {
132
		continue;
133
	}
134
	if ( false === strpos( $key, '{}' ) ) {
135
		preg_match( '/\[(.*)\] - (.*)/', $key, $matches );
136
		$changelog .= "= {$matches[1]} =\n\nRelease date: {$matches[2]}\n\n";
137
	} else {
138
		$tmp = explode( '{}', $key );
139
		$changelog .= "* {$tmp[1]}\n" . str_replace( '- ', '  * ', $notice );
140
	}
141
}
142
143
// -----------------------------------------------------------------------------
144
// Extract README details
145
$sections = [];
146
147
$readme = parse_markdown( __DIR__ . '/../README.md' );
148
149
$header = explode('=======================', $readme['default'] ?? '');
150
$heading = end($header);
151
$keyFeature = $readme['Key features'] ?? '';
152
$configuration = $readme['Configuration'] ?? '';
153
$documentation = '';
154
$installation = '';
155
foreach ( $readme as $key => $bloc ) {
156
	$tmp = [];
157
	if (false !== strpos( $key, '{}' )) {
158
		$tmp = explode( '{}', $key );
159
	}
160 View Code Duplication
	if (0 === strpos($key, 'Usage')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
		$documentation .= (count($tmp) > 1 ? str_repeat('#', count($tmp)+1).' '.end($tmp).PHP_EOL : '').$bloc;
162
	}
163 View Code Duplication
	if (0 === strpos($key, 'Installation')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
		$installation .= (count($tmp) > 1 ? str_repeat('#', count($tmp)+1).' '.end($tmp).PHP_EOL : '').$bloc;
165
	}
166
}
167
168
// -----------------------------------------------------------------------------
169
// Export template
170
echo str_replace([
171
    '%tags%',
172
    '%heading%',
173
    '%key-features%',
174
    '%configuration%',
175
    '%documentation%',
176
    '%installation%',
177
    '%changelog%',
178
    '%upgrade%',
179
    '%upgrade%',
180
], [
181
    $tags,
182
    trim($heading),
183
    trim($keyFeature),
184
    trim($configuration),
185
    trim($documentation),
186
    trim($installation),
187
    $changelog,
188
    $notices
189
], $template);
190