Skip to content

Commit b30a2e4

Browse files
authored
Implement PHP 7.0 features (#104)
* Added type hinting and return types * Added type hinting and return types to pattern expanders, changed array syntax * Added CS Fixer dependency, CS fixes
1 parent 8cfa24b commit b30a2e4

File tree

105 files changed

+1450
-1960
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+1450
-1960
lines changed

.php_cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
return PhpCsFixer\Config::create()
3+
->setRules([
4+
'@PSR2' => true,
5+
'declare_strict_types' => true,
6+
'array_syntax' => ['syntax' => 'short'],
7+
'blank_line_after_opening_tag' => true,
8+
'single_blank_line_before_namespace' => true,
9+
'no_unused_imports' => true
10+
])
11+
->setFinder(
12+
PhpCsFixer\Finder::create()
13+
->in(__DIR__ . '/src')
14+
->in(__DIR__ . '/tests')
15+
)->setRiskyAllowed(true)
16+
->setUsingCache(false);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"openlss/lib-array2xml": "~0.0.9"
2525
},
2626
"require-dev": {
27-
"phpunit/phpunit": "^6.0"
27+
"phpunit/phpunit": "^6.0",
28+
"friendsofphp/php-cs-fixer": "^2.4"
2829
},
2930
"autoload": {
3031
"psr-4": {

src/AST/Expander.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,37 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\AST;
46

57
final class Expander implements Node
68
{
7-
/**
8-
* @var
9-
*/
109
private $name;
1110

12-
/**
13-
* @var array
14-
*/
1511
private $arguments;
1612

17-
/**
18-
* @param $name
19-
*/
20-
public function __construct($name)
13+
public function __construct(string $name)
2114
{
2215
$this->name = $name;
23-
$this->arguments = array();
16+
$this->arguments = [];
2417
}
2518

26-
/**
27-
* @return mixed
28-
*/
29-
public function getName()
19+
public function getName() : string
3020
{
3121
return $this->name;
3222
}
3323

34-
/**
35-
* @param $argument
36-
*/
3724
public function addArgument($argument)
3825
{
3926
$this->arguments[] = $argument;
4027
}
4128

42-
/**
43-
* @return bool
44-
*/
45-
public function hasArguments()
29+
public function hasArguments() : bool
4630
{
4731
return (boolean) count($this->arguments);
4832
}
4933

50-
/**
51-
* @return array
52-
*/
53-
public function getArguments()
34+
public function getArguments() : array
5435
{
5536
return $this->arguments;
5637
}

src/AST/Node.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\AST;
46

57
interface Node
68
{
7-
89
}

src/AST/Pattern.php

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,39 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\AST;
46

57
final class Pattern implements Node
68
{
7-
/**
8-
* @var Type
9-
*/
109
private $type;
1110

12-
/**
13-
* @var Expander[]|array
14-
*/
1511
private $expanders;
1612

17-
/**
18-
* @param Type $type
19-
*/
2013
public function __construct(Type $type)
2114
{
22-
$this->expanders = array();
15+
$this->expanders = [];
2316
$this->type = $type;
2417
}
2518

26-
/**
27-
* @return Type
28-
*/
29-
public function getType()
19+
public function getType() : Type
3020
{
3121
return $this->type;
3222
}
3323

34-
/**
35-
* @return bool
36-
*/
37-
public function hasExpanders()
24+
public function hasExpanders() : bool
3825
{
3926
return (boolean) count($this->expanders);
4027
}
4128

4229
/**
43-
* @return Expander[]|array
30+
* @return Expander[]
4431
*/
45-
public function getExpanders()
32+
public function getExpanders() : array
4633
{
4734
return $this->expanders;
4835
}
4936

50-
/**
51-
* @param Expander $expander
52-
*/
5337
public function addExpander(Expander $expander)
5438
{
5539
$this->expanders[] = $expander;

src/AST/Type.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\AST;
46

57
final class Type implements Node
68
{
7-
/**
8-
* @var string
9-
*/
109
private $type;
1110

12-
/**
13-
* @param string $type
14-
*/
15-
public function __construct($type)
11+
public function __construct(string $type)
1612
{
1713
$this->type = $type;
1814
}
1915

20-
public function __toString()
16+
public function __toString() : string
2117
{
2218
return $this->type;
2319
}

src/Exception/Exception.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\Exception;
46

57
class Exception extends \Exception

src/Exception/InvalidArgumentException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\Exception;
46

57
class InvalidArgumentException extends Exception

src/Exception/InvalidExpanderTypeException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\Exception;
46

57
class InvalidExpanderTypeException extends Exception

src/Exception/PatternException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\Exception;
46

57
class PatternException extends Exception
68
{
7-
public static function syntaxError($message, $previous = null)
9+
public static function syntaxError(string $message, Exception $previous = null)
810
{
911
return new self('[Syntax Error] ' . $message, 0, $previous);
1012
}

src/Exception/UnknownExpanderClassException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\Exception;
46

57
class UnknownExpanderClassException extends Exception

src/Exception/UnknownExpanderException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\Exception;
46

57
class UnknownExpanderException extends Exception

src/Exception/UnknownTypeException.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\Exception;
46

57
class UnknownTypeException extends Exception
68
{
79
private $type;
810

9-
public function __construct($type)
11+
public function __construct(string $type)
1012
{
1113
$this->type = "@" . $type . "@";
1214
parent::__construct(sprintf("Type pattern \"%s\" is not supported.", $this->type), 0, null);
1315
}
1416

15-
/**
16-
* @return mixed
17-
*/
18-
public function getType()
17+
public function getType() : string
1918
{
2019
return $this->type;
2120
}

src/Factory.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher;
46

57
interface Factory
68
{
7-
/**
8-
* @return Matcher
9-
*/
10-
public function createMatcher();
9+
public function createMatcher() : Matcher;
1110
}

src/Factory/SimpleFactory.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Coduo\PHPMatcher\Factory;
46

57
use Coduo\PHPMatcher\Factory;
@@ -9,64 +11,55 @@
911

1012
class SimpleFactory implements Factory
1113
{
12-
/**
13-
* @return Matcher
14-
*/
15-
public function createMatcher()
14+
public function createMatcher() : Matcher
1615
{
1716
return new Matcher($this->buildMatchers());
1817
}
1918

20-
/**
21-
* @return Matcher\ChainMatcher
22-
*/
23-
protected function buildMatchers()
19+
protected function buildMatchers() : Matcher\ChainMatcher
2420
{
2521
$scalarMatchers = $this->buildScalarMatchers();
2622
$orMatcher = $this->buildOrMatcher();
2723

28-
$chainMatcher = new Matcher\ChainMatcher(array(
24+
$chainMatcher = new Matcher\ChainMatcher([
2925
$scalarMatchers,
3026
$orMatcher,
3127
new Matcher\JsonMatcher($orMatcher),
3228
new Matcher\XmlMatcher($orMatcher),
3329
new Matcher\TextMatcher($scalarMatchers, $this->buildParser())
34-
));
30+
]);
3531

3632
return $chainMatcher;
3733
}
3834

39-
/**
40-
* @return Matcher\ChainMatcher
41-
*/
42-
protected function buildOrMatcher()
35+
protected function buildOrMatcher() : Matcher\ChainMatcher
4336
{
4437
$scalarMatchers = $this->buildScalarMatchers();
4538
$orMatcher = new Matcher\OrMatcher($scalarMatchers);
4639
$arrayMatcher = new Matcher\ArrayMatcher(
47-
new Matcher\ChainMatcher(array(
40+
new Matcher\ChainMatcher([
4841
$orMatcher,
4942
$scalarMatchers
50-
)),
43+
]),
5144
$this->buildParser()
5245
);
5346

54-
$chainMatcher = new Matcher\ChainMatcher(array(
47+
$chainMatcher = new Matcher\ChainMatcher([
5548
$orMatcher,
5649
$arrayMatcher,
57-
));
50+
]);
5851

5952
return $chainMatcher;
6053
}
6154

6255
/**
6356
* @return Matcher\ChainMatcher
6457
*/
65-
protected function buildScalarMatchers()
58+
protected function buildScalarMatchers() : Matcher\ChainMatcher
6659
{
6760
$parser = $this->buildParser();
6861

69-
return new Matcher\ChainMatcher(array(
62+
return new Matcher\ChainMatcher([
7063
new Matcher\CallbackMatcher(),
7164
new Matcher\ExpressionMatcher(),
7265
new Matcher\NullMatcher(),
@@ -78,13 +71,10 @@ protected function buildScalarMatchers()
7871
new Matcher\ScalarMatcher(),
7972
new Matcher\WildcardMatcher(),
8073
new Matcher\UuidMatcher(),
81-
));
74+
]);
8275
}
8376

84-
/**
85-
* @return Parser
86-
*/
87-
protected function buildParser()
77+
protected function buildParser() : Parser
8878
{
8979
return new Parser(new Lexer(), new Parser\ExpanderInitializer());
9080
}

0 commit comments

Comments
 (0)