Skip to content

Commit 109870f

Browse files
authored
Merge pull request #55 from patinthehat/v2
Add PHP 8-only support (v2)
2 parents eda93b9 + d524b4e commit 109870f

18 files changed

+157
-214
lines changed

.github/workflows/php-cs-fixer.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Check & fix styling
2+
3+
on: [push]
4+
5+
jobs:
6+
php-cs-fixer:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Checkout code
11+
uses: actions/checkout@v2
12+
with:
13+
ref: ${{ github.head_ref }}
14+
15+
- name: Run PHP CS Fixer
16+
uses: docker://oskarstark/php-cs-fixer-ga
17+
with:
18+
args: --config=.php_cs.dist --allow-risky=yes
19+
20+
- name: Commit changes
21+
uses: stefanzweifel/git-auto-commit-action@v4
22+
with:
23+
commit_message: Fix styling

.github/workflows/run-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: true
1010
matrix:
1111
os: [ubuntu-latest]
12-
php: [8.0, 7.4]
12+
php: [8.0]
1313
dependency-version: [prefer-lowest, prefer-stable]
1414

1515
name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ build
22
composer.lock
33
docs
44
vendor
5+
.php_cs.cache

.php_cs.dist

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
$finder = Symfony\Component\Finder\Finder::create()
4+
->in([
5+
__DIR__ . '/src',
6+
__DIR__ . '/tests',
7+
])
8+
->name('*.php')
9+
->notName('*.blade.php')
10+
->ignoreDotFiles(true)
11+
->ignoreVCS(true);
12+
13+
return PhpCsFixer\Config::create()
14+
->setRules([
15+
'@PSR2' => true,
16+
'array_syntax' => ['syntax' => 'short'],
17+
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
18+
'no_unused_imports' => true,
19+
'not_operator_with_successor_space' => true,
20+
'trailing_comma_in_multiline_array' => true,
21+
'phpdoc_scalar' => true,
22+
'unary_operator_spaces' => true,
23+
'binary_operator_spaces' => true,
24+
'blank_line_before_statement' => [
25+
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
26+
],
27+
'phpdoc_single_line_var_spacing' => true,
28+
'phpdoc_var_without_name' => true,
29+
'class_attributes_separation' => [
30+
'elements' => [
31+
'method',
32+
],
33+
],
34+
'method_argument_space' => [
35+
'on_multiline' => 'ensure_fully_multiline',
36+
'keep_multiple_spaces_after_comma' => true,
37+
],
38+
'single_trait_insert_per_statement' => true,
39+
])
40+
->setFinder($finder);

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All Notable changes to `regex` will be documented in this file
44

5+
## 2.0.0 - unreleased
6+
7+
- require PHP 8+
8+
- drop support for PHP 7.x
9+
- convert syntax to PHP 8
10+
- move exceptions to "Exceptions" directory to match structure of other packages
11+
512
## 1.4.2 - 2020-11-04
613

714
- add support for PHP 8.0

CONTRIBUTING.md

-55
This file was deleted.

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,23 @@ Patterns, replacements and subjects can also be arrays. `Regex::replace` behaves
168168

169169
If anything goes wrong in a `Regex` method, a `RegexFailed` exception gets thrown. No need for checking `preg_last_error()`.
170170

171-
## Changelog
172-
173-
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
174-
175171
## Testing
176172

177173
``` bash
178174
$ composer test
179175
```
180176

177+
## Changelog
178+
179+
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
180+
181181
## Contributing
182182

183-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
183+
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
184184

185-
## Security
185+
## Security Vulnerabilities
186186

187-
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
187+
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
188188

189189
## Postcardware
190190

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020
],
2121
"require": {
22-
"php" : "^7.3|^8.0"
22+
"php" : "^8.0"
2323
},
2424
"require-dev": {
2525
"phpunit/phpunit": "^9.3"

src/RegexFailed.php src/Exceptions/RegexFailed.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<?php
22

3-
namespace Spatie\Regex;
3+
namespace Spatie\Regex\Exceptions;
44

55
use Exception;
66

77
class RegexFailed extends Exception
88
{
9-
public static function match(string $pattern, string $subject, string $message): self
9+
public static function match(string $pattern, string $subject, string $message): static
1010
{
1111
$subject = static::trimString($subject);
1212

1313
return new static("Error matching pattern `{$pattern}` with subject `{$subject}`. {$message}");
1414
}
1515

16-
public static function replace(string $pattern, string $subject, string $message): self
16+
public static function replace(string $pattern, string $subject, string $message): static
1717
{
1818
$subject = static::trimString($subject);
1919

2020
return new static("Error replacing pattern `{$pattern}` in subject `{$subject}`. {$message}");
2121
}
2222

23-
public static function groupDoesntExist(string $pattern, string $subject, $group): self
23+
public static function groupDoesntExist(string $pattern, string $subject, $group): static
2424
{
2525
return new static("Pattern `{$pattern}` with subject `{$subject}` didn't capture a group named {$group}");
2626
}

src/Helpers/Arr.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ public static function transpose(array $array): array
3333
return $result;
3434
}
3535

36-
/**
37-
* @param array $array
38-
*
39-
* @return mixed
40-
*/
41-
public static function first(array $array)
36+
public static function first(array $array): mixed
4237
{
4338
return reset($array);
4439
}

src/MatchAllResult.php

+10-28
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,21 @@
33
namespace Spatie\Regex;
44

55
use Exception;
6+
use Spatie\Regex\Exceptions\RegexFailed;
67
use Spatie\Regex\Helpers\Arr;
78

89
class MatchAllResult extends RegexResult
910
{
10-
/** @var string */
11-
protected $pattern;
12-
13-
/** @var string */
14-
protected $subject;
15-
16-
/** @var bool */
17-
protected $hasMatch;
18-
19-
/** @var array */
20-
protected $matches;
21-
22-
public function __construct(string $pattern, string $subject, bool $result, array $matches)
23-
{
24-
$this->pattern = $pattern;
25-
$this->subject = $subject;
26-
$this->hasMatch = $result;
27-
$this->matches = $matches;
11+
public function __construct(
12+
protected string $pattern,
13+
protected string $subject,
14+
protected bool $result,
15+
protected array $matches,
16+
) {
17+
//
2818
}
2919

30-
/**
31-
* @param string $pattern
32-
* @param string $subject
33-
*
34-
* @return static
35-
*
36-
* @throws \Spatie\Regex\RegexFailed
37-
*/
38-
public static function for(string $pattern, string $subject)
20+
public static function for(string $pattern, string $subject): static
3921
{
4022
$matches = [];
4123

@@ -54,7 +36,7 @@ public static function for(string $pattern, string $subject)
5436

5537
public function hasMatch(): bool
5638
{
57-
return $this->hasMatch;
39+
return $this->result;
5840
}
5941

6042
/**

src/MatchResult.php

+14-40
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,20 @@
33
namespace Spatie\Regex;
44

55
use Exception;
6+
use Spatie\Regex\Exceptions\RegexFailed;
67

78
class MatchResult extends RegexResult
89
{
9-
/** @var string */
10-
protected $pattern;
11-
12-
/** @var string */
13-
protected $subject;
14-
15-
/** @var bool */
16-
protected $hasMatch;
17-
18-
/** @var array */
19-
protected $matches;
20-
21-
public function __construct(string $pattern, string $subject, bool $hasMatch, array $matches)
22-
{
23-
$this->pattern = $pattern;
24-
$this->subject = $subject;
25-
$this->hasMatch = $hasMatch;
26-
$this->matches = $matches;
10+
public function __construct(
11+
protected string $pattern,
12+
protected string $subject,
13+
protected bool $hasMatch,
14+
protected array $matches,
15+
) {
16+
//
2717
}
2818

29-
/**
30-
* @param string $pattern
31-
* @param string $subject
32-
*
33-
* @return static
34-
*
35-
* @throws \Spatie\Regex\RegexFailed
36-
*/
37-
public static function for(string $pattern, string $subject)
19+
public static function for(string $pattern, string $subject): static
3820
{
3921
$matches = [];
4022

@@ -56,20 +38,12 @@ public function hasMatch(): bool
5638
return $this->hasMatch;
5739
}
5840

59-
/**
60-
* @return string|null
61-
*/
62-
public function result()
41+
public function result(): ?string
6342
{
6443
return $this->matches[0] ?? null;
6544
}
6645

67-
/**
68-
* @param string $default
69-
*
70-
* @return string
71-
*/
72-
public function resultOr($default)
46+
public function resultOr(string $default): string
7347
{
7448
return $this->result() ?? $default;
7549
}
@@ -83,7 +57,7 @@ public function resultOr($default)
8357
*
8458
* @throws RegexFailed
8559
*/
86-
public function group($group): string
60+
public function group(int | string $group): string
8761
{
8862
if (! isset($this->matches[$group])) {
8963
throw RegexFailed::groupDoesntExist($this->pattern, $this->subject, $group);
@@ -110,7 +84,7 @@ public function groups(): array
11084
*
11185
* @return string
11286
*/
113-
public function groupOr($group, $default): string
87+
public function groupOr(int | string $group, string $default): string
11488
{
11589
try {
11690
return $this->group($group);
@@ -128,7 +102,7 @@ public function groupOr($group, $default): string
128102
*
129103
* @throws RegexFailed
130104
*/
131-
public function namedGroup($group): string
105+
public function namedGroup(int | string $group): string
132106
{
133107
return $this->group($group);
134108
}

0 commit comments

Comments
 (0)