File tree 7 files changed +108
-16
lines changed
7 files changed +108
-16
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * An exception thrown by PHP_CodeSniffer when it encounters an error in the XML document being processed
4
+ * by one of the documentation Generators.
5
+ *
6
+ * @author Juliette Reinders Folmer <[email protected] >
7
+ * @copyright 2024 PHPCSStandards and contributors
8
+ * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
9
+ */
10
+
11
+ namespace PHP_CodeSniffer \Exceptions ;
12
+
13
+ use DomainException ;
14
+
15
+ class GeneratorException extends DomainException
16
+ {
17
+
18
+ }//end class
Original file line number Diff line number Diff line change 15
15
namespace PHP_CodeSniffer \Generators ;
16
16
17
17
use DOMDocument ;
18
+ use DOMElement ;
18
19
use DOMNode ;
19
20
use PHP_CodeSniffer \Autoload ;
21
+ use PHP_CodeSniffer \Exceptions \GeneratorException ;
20
22
use PHP_CodeSniffer \Ruleset ;
21
23
22
24
abstract class Generator
@@ -112,13 +114,22 @@ protected function getTitle(DOMNode $doc)
112
114
*
113
115
* @return void
114
116
* @see processSniff()
117
+ *
118
+ * @throws \PHP_CodeSniffer\Exceptions\GeneratorException If there is no <documentation> element
119
+ * in the XML document.
115
120
*/
116
121
public function generate ()
117
122
{
118
123
foreach ($ this ->docFiles as $ file ) {
119
124
$ doc = new DOMDocument ();
120
125
$ doc ->load ($ file );
121
126
$ documentation = $ doc ->getElementsByTagName ('documentation ' )->item (0 );
127
+ if (($ documentation instanceof DOMNode) === false ) {
128
+ throw new GeneratorException (
129
+ 'Missing top-level <documentation> element in XML documentation file ' .$ file
130
+ );
131
+ }
132
+
122
133
$ this ->processSniff ($ documentation );
123
134
}
124
135
Original file line number Diff line number Diff line change 19
19
use DOMElement ;
20
20
use DOMNode ;
21
21
use PHP_CodeSniffer \Config ;
22
+ use PHP_CodeSniffer \Exceptions \GeneratorException ;
22
23
23
24
class HTML extends Generator
24
25
{
@@ -126,6 +127,9 @@ class HTML extends Generator
126
127
*
127
128
* @return void
128
129
* @see processSniff()
130
+ *
131
+ * @throws \PHP_CodeSniffer\Exceptions\GeneratorException If there is no <documentation> element
132
+ * in the XML document.
129
133
*/
130
134
public function generate ()
131
135
{
@@ -134,10 +138,18 @@ public function generate()
134
138
}
135
139
136
140
ob_start ();
137
- parent ::generate ();
141
+ try {
142
+ parent ::generate ();
143
+ $ content = ob_get_clean ();
144
+ } catch (GeneratorException $ e ) {
145
+ ob_end_clean ();
146
+ $ content = '' ;
147
+ }
138
148
139
- $ content = ob_get_contents ();
140
- ob_end_clean ();
149
+ // If an exception was caught, rethrow it outside of the output buffer.
150
+ if (isset ($ e ) === true ) {
151
+ throw $ e ;
152
+ }
141
153
142
154
// Clear anchor cache after Documentation generation.
143
155
// The anchor generation for the TOC anchor links will use the same logic, so should end up with the same unique slugs.
Original file line number Diff line number Diff line change 14
14
use DOMElement ;
15
15
use DOMNode ;
16
16
use PHP_CodeSniffer \Config ;
17
+ use PHP_CodeSniffer \Exceptions \GeneratorException ;
17
18
18
19
class Markdown extends Generator
19
20
{
@@ -24,6 +25,9 @@ class Markdown extends Generator
24
25
*
25
26
* @return void
26
27
* @see processSniff()
28
+ *
29
+ * @throws \PHP_CodeSniffer\Exceptions\GeneratorException If there is no <documentation> element
30
+ * in the XML document.
27
31
*/
28
32
public function generate ()
29
33
{
@@ -32,10 +36,18 @@ public function generate()
32
36
}
33
37
34
38
ob_start ();
35
- parent ::generate ();
39
+ try {
40
+ parent ::generate ();
41
+ $ content = ob_get_clean ();
42
+ } catch (GeneratorException $ e ) {
43
+ ob_end_clean ();
44
+ $ content = '' ;
45
+ }
36
46
37
- $ content = ob_get_contents ();
38
- ob_end_clean ();
47
+ // If an exception was caught, rethrow it outside of the output buffer.
48
+ if (isset ($ e ) === true ) {
49
+ throw $ e ;
50
+ }
39
51
40
52
if (trim ($ content ) !== '' ) {
41
53
echo $ this ->getFormattedHeader ();
Original file line number Diff line number Diff line change 8
8
9
9
namespace PHP_CodeSniffer \Tests \Core \Generators ;
10
10
11
+ use PHP_CodeSniffer \Exceptions \GeneratorException ;
11
12
use PHP_CodeSniffer \Ruleset ;
12
13
use PHP_CodeSniffer \Tests \ConfigDouble ;
13
14
use PHP_CodeSniffer \Tests \Core \Generators \Fixtures \MockGenerator ;
@@ -94,8 +95,6 @@ public static function dataConstructor()
94
95
/**
95
96
* Verify that an XML doc which isn't valid documentation yields an Exception to warn devs.
96
97
*
97
- * This should not be hidden via defensive coding!
98
- *
99
98
* @return void
100
99
*/
101
100
public function testGeneratingInvalidDocsResultsInException ()
@@ -105,14 +104,8 @@ public function testGeneratingInvalidDocsResultsInException()
105
104
$ config = new ConfigDouble (["--standard= $ standard " ]);
106
105
$ ruleset = new Ruleset ($ config );
107
106
108
- if (PHP_VERSION_ID >= 80000 ) {
109
- $ message = 'processSniff(): Argument #1 ($doc) must be of type DOMNode, null given ' ;
110
- } else {
111
- $ message = 'processSniff() must be an instance of DOMNode, null given ' ;
112
- }
113
-
114
- $ this ->expectException ('TypeError ' );
115
- $ this ->expectExceptionMessage ($ message );
107
+ $ this ->expectException (GeneratorException::class);
108
+ $ this ->expectExceptionMessage ('Missing top-level <documentation> element in XML documentation file ' );
116
109
117
110
$ generator = new MockGenerator ($ ruleset );
118
111
$ generator ->generate ();
Original file line number Diff line number Diff line change 8
8
9
9
namespace PHP_CodeSniffer \Tests \Core \Generators ;
10
10
11
+ use PHP_CodeSniffer \Exceptions \GeneratorException ;
12
+ use PHP_CodeSniffer \Generators \HTML ;
11
13
use PHP_CodeSniffer \Ruleset ;
12
14
use PHP_CodeSniffer \Tests \ConfigDouble ;
13
15
use PHP_CodeSniffer \Tests \Core \Generators \Fixtures \HTMLDouble ;
@@ -23,6 +25,27 @@ final class HTMLTest extends TestCase
23
25
{
24
26
25
27
28
+ /**
29
+ * Verify that an XML doc which isn't valid documentation yields an Exception to warn devs.
30
+ *
31
+ * @return void
32
+ */
33
+ public function testGeneratingInvalidDocsResultsInException ()
34
+ {
35
+ // Set up the ruleset.
36
+ $ standard = __DIR__ .'/NoValidDocsTest.xml ' ;
37
+ $ config = new ConfigDouble (["--standard= $ standard " ]);
38
+ $ ruleset = new Ruleset ($ config );
39
+
40
+ $ this ->expectException (GeneratorException::class);
41
+ $ this ->expectExceptionMessage ('Missing top-level <documentation> element in XML documentation file ' );
42
+
43
+ $ generator = new HTML ($ ruleset );
44
+ $ generator ->generate ();
45
+
46
+ }//end testGeneratingInvalidDocsResultsInException()
47
+
48
+
26
49
/**
27
50
* Test the generated docs.
28
51
*
Original file line number Diff line number Diff line change 8
8
9
9
namespace PHP_CodeSniffer \Tests \Core \Generators ;
10
10
11
+ use PHP_CodeSniffer \Exceptions \GeneratorException ;
12
+ use PHP_CodeSniffer \Generators \Markdown ;
11
13
use PHP_CodeSniffer \Ruleset ;
12
14
use PHP_CodeSniffer \Tests \ConfigDouble ;
13
15
use PHP_CodeSniffer \Tests \Core \Generators \Fixtures \MarkdownDouble ;
@@ -23,6 +25,27 @@ final class MarkdownTest extends TestCase
23
25
{
24
26
25
27
28
+ /**
29
+ * Verify that an XML doc which isn't valid documentation yields an Exception to warn devs.
30
+ *
31
+ * @return void
32
+ */
33
+ public function testGeneratingInvalidDocsResultsInException ()
34
+ {
35
+ // Set up the ruleset.
36
+ $ standard = __DIR__ .'/NoValidDocsTest.xml ' ;
37
+ $ config = new ConfigDouble (["--standard= $ standard " ]);
38
+ $ ruleset = new Ruleset ($ config );
39
+
40
+ $ this ->expectException (GeneratorException::class);
41
+ $ this ->expectExceptionMessage ('Missing top-level <documentation> element in XML documentation file ' );
42
+
43
+ $ generator = new Markdown ($ ruleset );
44
+ $ generator ->generate ();
45
+
46
+ }//end testGeneratingInvalidDocsResultsInException()
47
+
48
+
26
49
/**
27
50
* Test the generated docs.
28
51
*
You can’t perform that action at this time.
0 commit comments