-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from d3f3kt/master
Add error types option
- Loading branch information
Showing
6 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Sentry\SentryBundle; | ||
|
||
/** | ||
* Evaluate an error types expression. | ||
*/ | ||
class ErrorTypesParser | ||
{ | ||
private $expression = null; | ||
|
||
/** | ||
* Initialize ErrorParser | ||
* | ||
* @param string $expression Error Types e.g. E_ALL & ~E_DEPRECATED & ~E_NOTICE | ||
*/ | ||
public function __construct($expression) | ||
{ | ||
$this->expression = $expression; | ||
} | ||
|
||
/** | ||
* Parse and compute the error types expression | ||
* | ||
* @return int the parsed expression | ||
*/ | ||
public function parse() | ||
{ | ||
// convert constants to ints | ||
$this->expression = $this->convertErrorConstants($this->expression); | ||
$this->expression = str_replace( | ||
array(",", " "), | ||
array(".", ""), | ||
$this->expression | ||
); | ||
// remove anything which could be a security issue | ||
$this->expression = preg_replace("/[^\d.+*%^|&~<>\/()-]/", "", $this->expression); | ||
|
||
return $this->compute($this->expression); | ||
} | ||
|
||
|
||
/** | ||
* Converts error constants from string to int. | ||
* | ||
* @param string $expression e.g. E_ALL & ~E_DEPRECATED & ~E_NOTICE | ||
* @return string convertes expression e.g. 32767 & ~8192 & ~8 | ||
*/ | ||
private function convertErrorConstants($expression) | ||
{ | ||
$output = preg_replace_callback("/(E_[a-zA-Z_]+)/", function ($errorConstant) { | ||
if (defined($errorConstant[1])) { | ||
return constant($errorConstant[1]); | ||
} | ||
return $errorConstant[0]; | ||
}, $expression); | ||
|
||
return $output; | ||
} | ||
|
||
/** | ||
* Let PHP compute the prepared expression for us. | ||
* | ||
* @param string $expression prepared expression e.g. 32767&~8192&~8 | ||
* @return int computed expression e.g. 24567 | ||
*/ | ||
private function compute($expression) | ||
{ | ||
$compute = create_function("", "return " . $expression . ";"); | ||
|
||
return 0 + $compute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Sentry\SentryBundle\Test; | ||
|
||
use Sentry\SentryBundle\ErrorTypesParser; | ||
|
||
class ErrorTypesParserTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function test_error_types_parser() | ||
{ | ||
$ex = new ErrorTypesParser('E_ALL & ~E_DEPRECATED & ~E_NOTICE'); | ||
$this->assertEquals($ex->parse(), E_ALL & ~E_DEPRECATED & ~E_NOTICE); | ||
} | ||
} |