Code style and test fixes

refs #3777
This commit is contained in:
Jannis Moßhammer 2013-08-16 16:46:58 +02:00 committed by Eric Lippmann
parent c705f5d475
commit 4810a0d47e
2 changed files with 81 additions and 66 deletions

View File

@ -28,8 +28,6 @@
namespace Icinga\Config;
use \Zend_Config_Exception;
/**
* Edit the sections and keys of an ini in-place
*/
@ -52,7 +50,7 @@ class IniEditor
/**
* Create a new IniEditor
*
* @param string $content The content of the ini as string
* @param string $content The content of the ini as string
*/
public function __construct($content)
{
@ -62,9 +60,9 @@ class IniEditor
/**
* Set the value of the given key.
*
* @param array $key The key to set
* @param mixed $value The value to set
* @param string $section The section to insert to.
* @param array $key The key to set
* @param string $value The value to set
* @param array $section The section to insert to.
*/
public function set(array $key, $value, $section = null)
{
@ -80,8 +78,8 @@ class IniEditor
/**
* Reset the value of the given array element
*
* @param array $key The key of the array value
* @param string $section The section of the array.
* @param array $key The key of the array value
* @param array $section The section of the array.
*/
public function resetArrayElement(array $key, $section = null)
{
@ -94,9 +92,9 @@ class IniEditor
/**
* Set the value for an array element
*
* @param array $key The key of the property
* @param mixed $value The value of the property
* @param string $section The section to use
* @param array $key The key of the property
* @param string $value The value of the property
* @param array $section The section to use
*/
public function setArrayElement(array $key, $value, $section = null)
{
@ -119,14 +117,15 @@ class IniEditor
/**
* Get the line of an array element
*
* @param array $key The key of the property.
* @param string $section The section to use
* @param array $key The key of the property.
* @param $value The value
* @param $section The section to use
*
* @return int The line of the array element.
* @return The line of the array element.
*/
private function getArrayElement(array $key, $section = null)
{
$line = isset($section) ? $this->getSectionLine($section) +1 : 0;
$line = isset($section) ? $this->getSectionLine($section) + 1 : 0;
$index = array_pop($key);
$formatted = $this->formatKey($key);
for (; $line < count($this->text); $line++) {
@ -134,7 +133,7 @@ class IniEditor
if ($this->isSectionDeclaration($l)) {
return -1;
}
if (preg_match('/^\s*' . $formatted.'\[\]\s*=/', $l) === 1) {
if (preg_match('/^\s*' . $formatted . '\[\]\s*=/', $l) === 1) {
return $line;
}
if ($this->isPropertyDeclaration($l, array_merge($key, array($index)))) {
@ -147,8 +146,8 @@ class IniEditor
/**
* When it exists, set the key back to null
*
* @param array $key The key to reset
* @param string $section The section of the key
* @param array $key The key to reset
* @param array $section The section of the key
*/
public function reset(array $key, $section = null)
{
@ -162,15 +161,15 @@ class IniEditor
/**
* Create the section if it does not exist and set the properties
*
* @param string $section The section name
* @param string $extend The section that should be extended by this section
* @param string $section The section name
* @param array $extend The section that should be extended by this section
*/
public function setSection($section, $extend = null)
{
if (isset($extend)) {
$decl = '[' . $section . ' : ' . $extend.']';
} else {
$decl = '[' . $section.']';
$decl = '[' . $section . ']';
}
$line = $this->getSectionLine($section);
if ($line !== -1) {
@ -185,7 +184,7 @@ class IniEditor
/**
* Remove a section declaration
*
* @param string $section The section name
* @param string $section The section name
*/
public function removeSection($section)
{
@ -198,9 +197,9 @@ class IniEditor
/**
* Insert the key at the end of the corresponding section
*
* @param array $key The key to insert
* @param mixed $value The value to insert
* @param string $section
* @param array $key The key to insert
* @param mixed $value The value to insert
* @param array $key The key to insert
*/
private function insert(array $key, $value, $section = null)
{
@ -260,8 +259,8 @@ class IniEditor
/**
* Insert the text at line $lineNr
*
* @param int $lineNr The line nr the inserted line should have
* @param string $toInsert The text that will be inserted
* @param $lineNr The line nr the inserted line should have
* @param $toInsert The text that will be inserted
*/
private function insertAtLine($lineNr, $toInsert)
{
@ -271,14 +270,14 @@ class IniEditor
/**
* Update the line $lineNr
*
* @param int $lineNr The line number of the target line
* @param string $content The content to replace
* @param $lineNr The line number of the target line
* @param $toInsert The new line content
*/
private function updateLine($lineNr, $content)
{
$comment = $this->getComment($this->text[$lineNr]);
if (strlen($comment) > 0) {
$comment = ' ; ' . trim($comment);
$comment = " ; " . trim($comment);
}
$this->text[$lineNr] = str_pad($content, 43) . $comment;
}
@ -286,9 +285,9 @@ class IniEditor
/**
* Get the comment from the given line
*
* @param string $lineContent The content of the line
* @param $lineContent The content of the line
*
* @return string The extracted comment
* @return string The extracted comment
*/
private function getComment($lineContent)
{
@ -298,14 +297,14 @@ class IniEditor
*/
$cleaned = preg_replace('/^[^;"]*"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"/s', '', $lineContent);
$matches = mb_split(';', $cleaned, 2);
$matches = explode(';', $cleaned, 2);
return array_key_exists(1, $matches) ? $matches[1] : '';
}
/**
* Delete the line $lineNr
*
* @param int $lineNr The lineNr starting at 0
* @param $lineNr The lineNr starting at 0
*/
private function deleteLine($lineNr)
{
@ -315,10 +314,10 @@ class IniEditor
/**
* Format a key-value pair to an INI file-entry
*
* @param array $key The key
* @param mixed $value The value
* @param array $key The key to format
* @param string $value The value to format
*
* @return string The formatted key-value pair
* @return string The formatted key-value pair
*/
private function formatKeyValuePair(array $key, $value)
{
@ -328,8 +327,9 @@ class IniEditor
/**
* Format a key to an INI key
*
* @param array $key
* @return string
* @param array $key the key array to format
*
* @return string
*/
private function formatKey(array $key)
{
@ -339,14 +339,14 @@ class IniEditor
/**
* Get the first line after the given $section
*
* @param string $section The name of the section
* @param $section The name of the section
*
* @return int The line number of the section
* @return int The line number of the section
*/
private function getSectionEnd($section = null)
{
$i = 0;
$started = isset($section) ? false: true;
$started = isset($section) ? false : true;
foreach ($this->text as $line) {
if ($started && $this->isSectionDeclaration($line)) {
if ($i === 0) {
@ -374,15 +374,15 @@ class IniEditor
/**
* Check if the line contains the property declaration for a key
*
* @param string $lineContent The content of the line
* @param array $key The key this declaration is supposed to have
* @param string $lineContent The content of the line
* @param array $key The key this declaration is supposed to have
*
* @return bool True, when the lineContent is a property declaration
* @return boolean True, when the lineContent is a property declaration
*/
private function isPropertyDeclaration($lineContent, array $key)
{
return preg_match(
'/^\s*' . $this->formatKey($key) .'\s*=\s*/',
'/^\s*' . $this->formatKey($key) . '\s*=\s*/',
$lineContent
) === 1;
}
@ -390,15 +390,15 @@ class IniEditor
/**
* Check if the given line contains a section declaration
*
* @param string $lineContent The content of the line
* @param string $section The optional section name that will be assumed
* @param $lineContent The content of the line
* @param string $section The optional section name that will be assumed
*
* @return bool True, when the lineContent is a section declaration
* @return bool True, when the lineContent is a section declaration
*/
private function isSectionDeclaration($lineContent, $section = null)
{
if (isset($section)) {
return preg_match('/^\s*\[\s*'.$section.'\s*[\]:]/', $lineContent) === 1;
return preg_match('/^\s*\[\s*' . $section . '\s*[\]:]/', $lineContent) === 1;
} else {
return preg_match('/^\s*\[/', $lineContent) === 1;
}
@ -407,9 +407,9 @@ class IniEditor
/**
* Get the line where the section begins
*
* @param string $section The section
* @param $section The section
*
* @return int The line number
* @return int The line number
*/
private function getSectionLine($section)
{
@ -426,13 +426,14 @@ class IniEditor
/**
* Get the line number where the given key occurs
*
* @param array $keys The key and its parents
* @param string $section The section of the key
* @param array $keys The key and its parents
* @param $section The section of the key
*
* @return int The line number
*/
private function getKeyLine(array $keys, $section = null)
{
$key = implode($this->nestSeparator, $keys);
$inSection = isset($section) ? false : true;
$i = 0;
foreach ($this->text as $line) {
@ -453,7 +454,7 @@ class IniEditor
/**
* Get the last line number occurring in the text
*
* @return int The line number of the last line
* @return The line number of the last line
*/
private function getLastLine()
{
@ -463,9 +464,9 @@ class IniEditor
/**
* Insert a new element into a specific position of an array
*
* @param array $array The array to use
* @param int $pos The target position
* @param mixed $element The element to insert
* @param $array The array to use
* @param $pos The target position
* @param $element The element to insert
*
* @return array The changed array
*/
@ -478,12 +479,10 @@ class IniEditor
/**
* Remove an element from an array
*
* @param array $array The array to use
* @param mixed $pos The position to remove
*
* @return array
* @param $array The array to use
* @param $pos The position to remove
*/
private function removeFromArray(array $array, $pos)
private function removeFromArray($array, $pos)
{
unset($array[$pos]);
return array_values($array);
@ -492,9 +491,10 @@ class IniEditor
/**
* Prepare a value for INe
*
* @param mixed $value The value of the string
* @param $value The value of the string
*
* @return string The formatted value
*
* @return string The formatted value
* @throws Zend_Config_Exception
*/
private function formatValue($value)

View File

@ -87,7 +87,11 @@ class GeneralFormTest extends \Test\Icinga\Web\Form\BaseFormTest
)
)
);
$form->setResources(
array(
'db' => 'db'
)
);
$form->setConfigDir('/tmp');
$view = new Zend_View();
@ -117,7 +121,13 @@ class GeneralFormTest extends \Test\Icinga\Web\Form\BaseFormTest
)
);
$form->setConfigDir('/tmp');
$form->setResources(
array(
'db' => 'db'
)
);
$form->create();
$view = new Zend_View();
$this->assertFalse(
@ -146,6 +156,11 @@ class GeneralFormTest extends \Test\Icinga\Web\Form\BaseFormTest
)
);
$form->setConfigDir('/tmp');
$form->setResources(
array(
'db' => 'db'
)
);
$form->create();
$view = new Zend_View();