Skip to content

Commit 98c828c

Browse files
authored
Merge pull request #3 from battye/bugfix/hash-comment
Respect hashes as valid comments, migrate to GitHub Actions
2 parents 9927b29 + bf987bd commit 98c828c

File tree

5 files changed

+77
-13
lines changed

5 files changed

+77
-13
lines changed

.github/workflows/phpunit.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# .github/workflows/phpunit.yaml
2+
name: phpunit
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
tests:
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
php: ['5.6', '7.0', '7.1', '7.3', '7.4']
12+
13+
name: PHP ${{ matrix.php }} tests
14+
steps:
15+
- run: echo "This job for ${{ github.ref }} was automatically triggered by a ${{ github.event_name }} event on ${{ runner.os }}."
16+
17+
# basically git clone
18+
- uses: actions/checkout@v2
19+
20+
# use PHP of specific version
21+
- uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php }}
24+
coverage: none # disable xdebug, pcov
25+
26+
# if we 2 steps like this, we can better see if composer failed or tests
27+
- run: composer install --no-progress
28+
- run: vendor/bin/simple-phpunit tests/
29+
- run: echo "This job's status is ${{ job.status }}."

.travis.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ $result = parser::parse_regex($regex, $file);
5151

5252
## Tests
5353

54-
[![Latest Stable Version](https://poser.pugx.org/battye/php-array-parser/v/stable)](https://packagist.org/packages/battye/php-array-parser) [![Build Status](https://travis-ci.com/battye/php-array-parser.svg?branch=master)](https://travis-ci.com/battye/php-array-parser) [![Total Downloads](https://poser.pugx.org/battye/php-array-parser/downloads)](https://packagist.org/packages/battye/php-array-parser)
54+
[![Latest Stable Version](https://poser.pugx.org/battye/php-array-parser/v/stable)](https://packagist.org/packages/battye/php-array-parser) [![Total Downloads](https://poser.pugx.org/battye/php-array-parser/downloads)](https://packagist.org/packages/battye/php-array-parser) ![GitHub Actions CI](https://github.com/battye/php-array-parser/actions/workflows/phpunit.yaml/badge.svg?branch=master)
5555

5656
The unit tests provide good examples of how to utilise this library and can be found in the `tests/` directory. To execute the unit tests, run:
5757

src/parser.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ public function is_comment_block($code)
209209
// Long form block
210210
$long = (substr($code, 0, 2) == '/*' && substr($code, -2) == '*/');
211211

212-
return ($short || $long);
212+
// Hash form
213+
$hash = (substr($code, 0, 1) === '#');
214+
215+
return ($short || $long || $hash);
213216
}
214217

215218
/**
@@ -235,6 +238,9 @@ public function parse_array($square = false)
235238

236239
while (true)
237240
{
241+
// Immediately ignore any comments
242+
$this->ignore_comments();
243+
238244
if ($this->tokens->does_match(")") || $this->tokens->does_match("]"))
239245
{
240246
// Reached the end of the array
@@ -303,6 +309,10 @@ public function parse_array($square = false)
303309
{
304310
// Array key (key => value)
305311
$this->tokens->pop();
312+
313+
// Ignore comments in case someone put them on a new line within the array
314+
$this->ignore_comments();
315+
306316
$result[$string] = $this->parse_value();
307317
}
308318

tests/ArrayParserTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,42 @@ public function testCanDetectVariable()
9393
$this->assertArraySubset($expected_simple_output, $result);
9494
}
9595

96+
/**
97+
* Test that we can include all types of comments in unusual places
98+
*/
99+
public function testComments()
100+
{
101+
$string = "array(0 =>
102+
// upfront test comment
103+
# upfront test comment
104+
/* upfront test comment */
105+
array(
106+
'one' => 1, // expect 1
107+
/* internal test comment */
108+
'two' => 2, /* expect 2 */
109+
# internal test comment
110+
'three' => 3, # expect 3
111+
// internal test comment
112+
)
113+
# a strange place for a test comment
114+
)
115+
/*
116+
final test comment
117+
but multi-line
118+
*/
119+
;";
120+
121+
$tokens = new tokens($string);
122+
$parser = new parser($tokens);
123+
$result = $parser->parse_array();
124+
125+
$expected_output = [
126+
['one' => 1, 'two' => 2, 'three' => 3]
127+
];
128+
129+
$this->assertArraySubset($expected_output, $result);
130+
}
131+
96132
/**
97133
* Test that an event file is parsed correctly
98134
*/

0 commit comments

Comments
 (0)