8000 Support html+php (#22) · Nyholm/code-block-checker@d0ecde9 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0ecde9

Browse files
authored
Support html+php (symfony-tools#22)
* Support html+php * Bug fixes * Add "continue on errors"
1 parent a31d3cc commit d0ecde9

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

.github/workflows/static.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ jobs:
77
phpstan:
88
name: PHPStan
99
runs-on: Ubuntu-20.04
10+
continue-on-error: true
1011

1112
steps:
1213
- name: Checkout code
@@ -39,6 +40,7 @@ jobs:
3940
php-cs-fixer:
4041
name: PHP-CS-Fixer
4142
runs-on: Ubuntu-20.04
43+
continue-on-error: true
4244

4345
steps:
4446
- name: Checkout code
@@ -71,6 +73,8 @@ jobs:
7173
psalm:
7274
name: Psalm
7375
runs-on: Ubuntu-20.04
76+
continue-on-error: true
77+
7478
steps:
7579
- name: Checkout code
7680
uses: actions/checkout@v2
@@ -102,6 +106,7 @@ jobs:
102106
composer-normalize:
103107
name: Composer Normalize
104108
runs-on: Ubuntu-20.04
109+
continue-on-error: true
105110

106111
steps:
107112
- name: Set up PHP

src/Service/CodeValidator/PhpValidator.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@ class PhpValidator implements Validator
1212
public function validate(CodeNode $node, IssueCollection $issues): void
1313
{
1414
$language = $node->getLanguage() ?? ($node->isRaw() ? null : 'php');
15-
if (!in_array($language, ['php', 'php-symfony', 'php-standalone', 'php-annotations'])) {
15+
if (!in_array($language, ['php', 'php-symfony', 'php-standalone', 'php-annotations', 'html+php'])) {
1616
return;
1717
}
1818

1919
$file = sys_get_temp_dir().'/'.uniqid('doc_builder', true).'.php';
20-
$contents = $node->getValue();
21-
if (!preg_match('#(class|interface) [a-zA-Z]+#s', $contents) && preg_match('#(public|protected|private)( static)? (\$[a-z]+|function)#s', $contents)) {
22-
$contents = 'class Foobar {'.$contents.'}';
23-
}
2420

25-
// Allow us to use "..." as a placeholder
26-
$contents = str_replace('...', 'null', $contents);
27-
file_put_contents($file, '<?php'.PHP_EOL.$contents);
21+
file_put_contents($file, $this->getContents($node, $language));
2822

2923
$process = new Process(['php', '-l', $file]);
3024
$process->run();
@@ -40,4 +34,26 @@ public function validate(CodeNode $node, IssueCollection $issues): void
4034
}
4135
$issues->addIssue(new Issue($node, $text, 'Invalid syntax', $node->getEnvironment()->getCurrentFileName(), $line));
4236
}
37+
38+
private function getContents(CodeNode $node, string $language): string
39+
{
40+
$contents = $node->getValue();
41+
if ('html+php' === $language) {
42+
return $contents;
43+
}
44+
45+
if (!preg_match('#(class|interface) [a-zA-Z]+#s', $contents) && preg_match('#(public|protected|private)( static)? (\$[a-z]+|function)#s', $contents)) {
46+
$contents = 'class Foobar {'.$contents.'}';
47+
}
48+
49+
// Allow us to use "..." as a placeholder
50+
$contents = str_replace('...', 'null', $contents);
51+
52+
$lines = explode(PHP_EOL, $contents);
53+
if (!str_contains($lines[0] ?? '', '<?php') && !str_contains($lines[1] ?? '', '<?php') && !str_contains($lines[2] ?? '', '<?php')) {
54+
$contents = '<?php'.PHP_EOL.$contents;
55+
}
56+
57+
return $contents;
58+
}
4359
}

0 commit comments

Comments
 (0)
0