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

Get string/boolean/integer property fallback #750

Merged
merged 13 commits into from Apr 19, 2020
81 changes: 54 additions & 27 deletions src/main/php/PHPMD/AbstractRule.php
Expand Up @@ -298,51 +298,78 @@ public function addProperty($name, $value)
}

/**
* Returns the value of a configured property as a boolean or throws an
* exception when no property with <b>$name</b> exists.
* Returns the value of a configured property
*
* @param string $name
* @return boolean
* @throws \OutOfBoundsException When no property for <b>$name</b> exists.
* Throws an exception when no property with <b>$name</b> exists
* and no default value to fall back was given.
*
* @param string $name The name of the property, e.g. "ignore-whitespace".
* @param mixed $default An optional default value to fall back instead of throwing an exception.
* @return mixed The value of a configured property.
* @throws \OutOfBoundsException When no property for <b>$name</b> exists and
* no default value to fall back was given.
*/
public function getBooleanProperty($name)
protected function getProperty($name, $default = null)
{
if (isset($this->properties[$name])) {
return in_array($this->properties[$name], array('true', 'on', 1));
return $this->properties[$name];
}
if ($default !== null) {
return $default;
}

throw new \OutOfBoundsException('Property "' . $name . '" does not exist.');
}

/**
* Returns the value of a configured property as an integer or throws an
* exception when no property with <b>$name</b> exists.
* Returns the value of a configured property as a boolean
*
* @param string $name
* @return integer
* @throws \OutOfBoundsException When no property for <b>$name</b> exists.
* Throws an exception when no property with <b>$name</b> exists
* and no default value to fall back was given.
*
* @param string $name The name of the property, e.g. "ignore-whitespace".
* @param bool $default An optional default value to fall back instead of throwing an exception.
* @return bool The value of a configured property as a boolean.
* @throws \OutOfBoundsException When no property for <b>$name</b> exists and
* no default value to fall back was given.
*/
public function getIntProperty($name)
public function getBooleanProperty($name, $default = null)
{
if (isset($this->properties[$name])) {
return (int) $this->properties[$name];
}
throw new \OutOfBoundsException('Property "' . $name . '" does not exist.');
return in_array($this->getProperty($name, $default), array('true', 'on', 1), false);
}

/**
* Returns the raw string value of a configured property or throws an
* exception when no property with <b>$name</b> exists.
* Returns the value of a configured property as an integer
*
* @param string $name
* @return string
* @throws \OutOfBoundsException When no property for <b>$name</b> exists.
* Throws an exception when no property with <b>$name</b> exists
* and no default value to fall back was given.
*
* @param string $name The name of the property, e.g. "minimum".
* @param int $default An optional default value to fall back instead of throwing an exception.
* @return int The value of a configured property as an integer.
* @throws \OutOfBoundsException When no property for <b>$name</b> exists and
* no default value to fall back was given.
*/
public function getStringProperty($name)
public function getIntProperty($name, $default = null)
{
if (isset($this->properties[$name])) {
return $this->properties[$name];
}
throw new \OutOfBoundsException('Property "' . $name . '" does not exist.');
return (int)$this->getProperty($name, $default);
}

/**
* Returns the raw string value of a configured property
*
* Throws an exception when no property with <b>$name</b> exists
* and no default value to fall back was given.
*
* @param string $name The name of the property, e.g. "exceptions".
* @param string|null $default An optional default value to fall back instead of throwing an exception.
* @return string The raw string value of a configured property.
* @throws \OutOfBoundsException When no property for <b>$name</b> exists and
* no default value to fall back was given.
*/
public function getStringProperty($name, $default = null)
{
return (string)$this->getProperty($name, $default);
}

/**
Expand Down
102 changes: 87 additions & 15 deletions src/test/php/PHPMD/RuleTest.php
Expand Up @@ -20,30 +20,20 @@
/**
* Test case for the {@link \PHPMD\AbstractRule} class.
*
* @covers \PHPMD\AbstractRule
* @coversDefaultClass \PHPMD\AbstractRule
*/
class RuleTest extends AbstractTest
{
/**
* testGetIntPropertyReturnsValueOfTypeInteger
*
* @return void
*/
public function testGetIntPropertyReturnsValueOfTypeInteger()
{
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');
$rule->addProperty(__FUNCTION__, '42.3');

$this->assertSame(42, $rule->getIntProperty(__FUNCTION__));
}

/**
* testGetBooleanPropertyReturnsTrueForStringValue1
*
* @return void
* @covers ::getBooleanProperty
* @covers ::getProperty
*/
public function testGetBooleanPropertyReturnsTrueForStringValue1()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');
$rule->addProperty(__FUNCTION__, '1');

Expand All @@ -54,9 +44,12 @@ public function testGetBooleanPropertyReturnsTrueForStringValue1()
* testGetBooleanPropertyReturnsTrueForStringValueOn
*
* @return void
* @covers ::getBooleanProperty
* @covers ::getProperty
*/
public function testGetBooleanPropertyReturnsTrueForStringValueOn()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');
$rule->addProperty(__FUNCTION__, 'on');

Expand All @@ -67,9 +60,12 @@ public function testGetBooleanPropertyReturnsTrueForStringValueOn()
* testGetBooleanPropertyReturnsTrueForStringValueTrue
*
* @return void
* @covers ::getBooleanProperty
* @covers ::getProperty
*/
public function testGetBooleanPropertyReturnsTrueForStringValueTrue()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');
$rule->addProperty(__FUNCTION__, 'true');

Expand All @@ -80,47 +76,105 @@ public function testGetBooleanPropertyReturnsTrueForStringValueTrue()
* testGetBooleanPropertyReturnsTrueForDifferentStringValue
*
* @return void
* @covers ::getBooleanProperty
* @covers ::getProperty
*/
public function testGetBooleanPropertyReturnsTrueForDifferentStringValue()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');
$rule->addProperty(__FUNCTION__, 'True');

$this->assertFalse($rule->getBooleanProperty(__FUNCTION__));
}

/**
* Tests the getBooleanProperty method with a fallback value
*
* @return void
* @covers ::getBooleanProperty
* @covers ::getProperty
*/
public function testGetBooleanPropertyReturnsFallbackString()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');

$this->assertTrue($rule->getBooleanProperty(__FUNCTION__, true));
}

/**
* testGetIntPropertyReturnsValueOfTypeInteger
*
* @return void
* @covers ::getIntProperty
* @covers ::getProperty
*/
public function testGetIntPropertyReturnsValueOfTypeInteger()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');
$rule->addProperty(__FUNCTION__, '42.3');

$this->assertSame(42, $rule->getIntProperty(__FUNCTION__));
}

/**
* testGetIntPropertyThrowsExceptionWhenNoPropertyForNameExists
*
* @return void
* @expectedException \OutOfBoundsException
* @covers ::getIntProperty
* @covers ::getProperty
*/
public function testGetIntPropertyThrowsExceptionWhenNoPropertyForNameExists()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');
$rule->getIntProperty(__FUNCTION__);
}

/**
* Tests the getIntProperty method with a fallback value
*
* @return void
* @covers ::getIntProperty
* @covers ::getProperty
*/
public function testGetIntPropertyReturnsFallbackString()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');

$this->assertSame(123, $rule->getIntProperty(__FUNCTION__, '123'));
}

/**
* testGetBooleanPropertyThrowsExceptionWhenNoPropertyForNameExists
*
* @return void
* @expectedException \OutOfBoundsException
* @covers ::getBooleanProperty
* @covers ::getProperty
*/
public function testGetBooleanPropertyThrowsExceptionWhenNoPropertyForNameExists()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');
$rule->getBooleanProperty(__FUNCTION__);
}

/**
* testStringPropertyThrowsExceptionWhenNoPropertyForNameExists
* testGetStringPropertyThrowsExceptionWhenNoPropertyForNameExists
*
* @return void
* @expectedException \OutOfBoundsException
* @covers ::getStringProperty
* @covers ::getProperty
*/
public function testGetStringPropertyThrowsExceptionWhenNoPropertyForNameExists()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');
$rule->getStringProperty(__FUNCTION__);
}
Expand All @@ -129,12 +183,30 @@ public function testGetStringPropertyThrowsExceptionWhenNoPropertyForNameExists(
* testGetStringPropertyReturnsStringValue
*
* @return void
* @covers ::getStringProperty
* @covers ::getProperty
*/
public function testGetStringPropertyReturnsString()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');
$rule->addProperty(__FUNCTION__, 'Fourty Two');

$this->assertSame('Fourty Two', $rule->getStringProperty(__FUNCTION__));
}

/**
* Tests the getStringProperty method with a fallback value
*
* @return void
* @covers ::getStringProperty
* @covers ::getProperty
*/
public function testGetStringPropertyReturnsFallbackString()
{
/** @var AbstractRule $rule */
$rule = $this->getMockForAbstractClass('PHPMD\\AbstractRule');

$this->assertSame('fallback', $rule->getStringProperty(__FUNCTION__, 'fallback'));
}
}