mirror of
https://github.com/danog/amp.git
synced 2024-11-26 20:15:00 +01:00
Suggest nearby property name in Struct error messages
This commit is contained in:
parent
2c3a3e65bd
commit
93eb8ad8de
@ -1,5 +1,6 @@
|
||||
### HEAD
|
||||
|
||||
- Suggest nearby property name in Struct error messages
|
||||
- Add offending generator key to error message when resolving coroutines
|
||||
that yield incorrect values to assist in debugging.
|
||||
|
||||
|
@ -9,6 +9,12 @@ namespace Amp;
|
||||
* nonexistent property names are read or written.
|
||||
*/
|
||||
trait Struct {
|
||||
/**
|
||||
* The minimum percentage [0-100] at which to recommend a similar property
|
||||
* name when generating error messages.
|
||||
*/
|
||||
private $__propertySuggestThreshold = 70;
|
||||
|
||||
final public function __get($property) {
|
||||
throw new \DomainException(
|
||||
$this->generateStructPropertyError($property)
|
||||
@ -22,6 +28,34 @@ trait Struct {
|
||||
}
|
||||
|
||||
private function generateStructPropertyError($property) {
|
||||
return sprintf("Struct property %s::%s does not exist", get_class($this), $property);
|
||||
$suggestion = $this->suggestPropertyName($property);
|
||||
$suggestStr = ($suggestion == "") ? "" : " ... did you mean \"{$suggestion}?\"";
|
||||
|
||||
return sprintf(
|
||||
"%s property \"%s\" does not exist%s",
|
||||
get_class($this),
|
||||
$property,
|
||||
$suggestStr
|
||||
);
|
||||
}
|
||||
|
||||
private function suggestPropertyName($badProperty) {
|
||||
$badProperty = strtolower($badProperty);
|
||||
$bestMatch = "";
|
||||
$bestMatchPercentage = 0.00;
|
||||
$byRefPercentage = 0.00;
|
||||
foreach ($this as $property => $value) {
|
||||
// Never suggest properties that begin with an underscore
|
||||
if ($property[0] === "_") {
|
||||
continue;
|
||||
}
|
||||
\similar_text($badProperty, strtolower($property), $byRefPercentage);
|
||||
if ($byRefPercentage > $bestMatchPercentage) {
|
||||
$bestMatchPercentage = $byRefPercentage;
|
||||
$bestMatch = $property;
|
||||
}
|
||||
}
|
||||
|
||||
return ($bestMatchPercentage >= $this->__propertySuggestThreshold) ? $bestMatch : "";
|
||||
}
|
||||
}
|
||||
|
52
test/StructTest.php
Normal file
52
test/StructTest.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Test;
|
||||
|
||||
use Amp\Watcher;
|
||||
|
||||
class StructTest extends \PHPUnit_Framework_TestCase {
|
||||
/**
|
||||
* @expectedException \DomainException
|
||||
* @expectedExceptionMessage Amp\Watcher property "callbac" does not exist ... did you mean "callback?"
|
||||
*/
|
||||
public function testSetErrorWithSuggestion() {
|
||||
$struct = new Watcher;
|
||||
$struct->callbac = function(){};
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \DomainException
|
||||
* @expectedExceptionMessage Amp\Watcher property "callbac" does not exist ... did you mean "callback?"
|
||||
*/
|
||||
public function testGetErrorWithSuggestion() {
|
||||
$struct = new Watcher;
|
||||
$test = $struct->callbac;
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \DomainException
|
||||
* @expectedExceptionMessage Amp\Watcher property "callZZZZZZZZZZZ" does not exist
|
||||
*/
|
||||
public function testSetErrorWithoutSuggestion() {
|
||||
$struct = new Watcher;
|
||||
$struct->callZZZZZZZZZZZ = "test";
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \DomainException
|
||||
* @expectedExceptionMessage Amp\Watcher property "callZZZZZZZZZZZ" does not exist
|
||||
*/
|
||||
public function testGetErrorWithoutSuggestion() {
|
||||
$struct = new Watcher;
|
||||
$test = $struct->callZZZZZZZZZZZ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \DomainException
|
||||
* @expectedExceptionMessage Amp\Watcher property "__propertySuggestThreshold" does not exist
|
||||
*/
|
||||
public function testSuggestionIgnoresPropertyStartingWithUnderscore() {
|
||||
$struct = new Watcher;
|
||||
$test = $struct->__propertySuggestThreshold;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user