Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PHP 7.0 features #104

Merged
merged 3 commits into from
Aug 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'declare_strict_types' => true,
'array_syntax' => ['syntax' => 'short'],
'blank_line_after_opening_tag' => true,
'single_blank_line_before_namespace' => true,
'no_unused_imports' => true
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
)->setRiskyAllowed(true)
->setUsingCache(false);
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"openlss/lib-array2xml": "~0.0.9"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
"phpunit/phpunit": "^6.0",
"friendsofphp/php-cs-fixer": "^2.4"
},
"autoload": {
"psr-4": {
Expand Down
33 changes: 7 additions & 26 deletions src/AST/Expander.php
Original file line number Diff line number Diff line change
@@ -1,56 +1,37 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\AST;

final class Expander implements Node
{
/**
* @var
*/
private $name;

/**
* @var array
*/
private $arguments;

/**
* @param $name
*/
public function __construct($name)
public function __construct(string $name)
{
$this->name = $name;
$this->arguments = array();
$this->arguments = [];
}

/**
* @return mixed
*/
public function getName()
public function getName() : string
{
return $this->name;
}

/**
* @param $argument
*/
public function addArgument($argument)
{
$this->arguments[] = $argument;
}

/**
* @return bool
*/
public function hasArguments()
public function hasArguments() : bool
{
return (boolean) count($this->arguments);
}

/**
* @return array
*/
public function getArguments()
public function getArguments() : array
{
return $this->arguments;
}
Expand Down
3 changes: 2 additions & 1 deletion src/AST/Node.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\AST;

interface Node
{

}
30 changes: 7 additions & 23 deletions src/AST/Pattern.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,39 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\AST;

final class Pattern implements Node
{
/**
* @var Type
*/
private $type;

/**
* @var Expander[]|array
*/
private $expanders;

/**
* @param Type $type
*/
public function __construct(Type $type)
{
$this->expanders = array();
$this->expanders = [];
$this->type = $type;
}

/**
* @return Type
*/
public function getType()
public function getType() : Type
{
return $this->type;
}

/**
* @return bool
*/
public function hasExpanders()
public function hasExpanders() : bool
{
return (boolean) count($this->expanders);
}

/**
* @return Expander[]|array
* @return Expander[]
*/
public function getExpanders()
public function getExpanders() : array
{
return $this->expanders;
}

/**
* @param Expander $expander
*/
public function addExpander(Expander $expander)
{
$this->expanders[] = $expander;
Expand Down
12 changes: 4 additions & 8 deletions src/AST/Type.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\AST;

final class Type implements Node
{
/**
* @var string
*/
private $type;

/**
* @param string $type
*/
public function __construct($type)
public function __construct(string $type)
{
$this->type = $type;
}

public function __toString()
public function __toString() : string
{
return $this->type;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/Exception.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Exception;

class Exception extends \Exception
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Exception;

class InvalidArgumentException extends Exception
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/InvalidExpanderTypeException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Exception;

class InvalidExpanderTypeException extends Exception
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/PatternException.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Exception;

class PatternException extends Exception
{
public static function syntaxError($message, $previous = null)
public static function syntaxError(string $message, Exception $previous = null)
{
return new self('[Syntax Error] ' . $message, 0, $previous);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/UnknownExpanderClassException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Exception;

class UnknownExpanderClassException extends Exception
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/UnknownExpanderException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Exception;

class UnknownExpanderException extends Exception
Expand Down
9 changes: 4 additions & 5 deletions src/Exception/UnknownTypeException.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Exception;

class UnknownTypeException extends Exception
{
private $type;

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

/**
* @return mixed
*/
public function getType()
public function getType() : string
{
return $this->type;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Factory.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher;

interface Factory
{
/**
* @return Matcher
*/
public function createMatcher();
public function createMatcher() : Matcher;
}
40 changes: 15 additions & 25 deletions src/Factory/SimpleFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Factory;

use Coduo\PHPMatcher\Factory;
Expand All @@ -9,64 +11,55 @@

class SimpleFactory implements Factory
{
/**
* @return Matcher
*/
public function createMatcher()
public function createMatcher() : Matcher
{
return new Matcher($this->buildMatchers());
}

/**
* @return Matcher\ChainMatcher
*/
protected function buildMatchers()
protected function buildMatchers() : Matcher\ChainMatcher
{
$scalarMatchers = $this->buildScalarMatchers();
$orMatcher = $this->buildOrMatcher();

$chainMatcher = new Matcher\ChainMatcher(array(
$chainMatcher = new Matcher\ChainMatcher([
$scalarMatchers,
$orMatcher,
new Matcher\JsonMatcher($orMatcher),
new Matcher\XmlMatcher($orMatcher),
new Matcher\TextMatcher($scalarMatchers, $this->buildParser())
));
]);

return $chainMatcher;
}

/**
* @return Matcher\ChainMatcher
*/
protected function buildOrMatcher()
protected function buildOrMatcher() : Matcher\ChainMatcher
{
$scalarMatchers = $this->buildScalarMatchers();
$orMatcher = new Matcher\OrMatcher($scalarMatchers);
$arrayMatcher = new Matcher\ArrayMatcher(
new Matcher\ChainMatcher(array(
new Matcher\ChainMatcher([
$orMatcher,
$scalarMatchers
)),
]),
$this->buildParser()
);

$chainMatcher = new Matcher\ChainMatcher(array(
$chainMatcher = new Matcher\ChainMatcher([
$orMatcher,
$arrayMatcher,
));
]);

return $chainMatcher;
}

/**
* @return Matcher\ChainMatcher
*/
protected function buildScalarMatchers()
protected function buildScalarMatchers() : Matcher\ChainMatcher
{
$parser = $this->buildParser();

return new Matcher\ChainMatcher(array(
return new Matcher\ChainMatcher([
new Matcher\CallbackMatcher(),
new Matcher\ExpressionMatcher(),
new Matcher\NullMatcher(),
Expand All @@ -78,13 +71,10 @@ protected function buildScalarMatchers()
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher(),
new Matcher\UuidMatcher(),
));
]);
}

/**
* @return Parser
*/
protected function buildParser()
protected function buildParser() : Parser
{
return new Parser(new Lexer(), new Parser\ExpanderInitializer());
}
Expand Down
Loading