Skip to content

Commit

Permalink
Code style and test fixes
Browse files Browse the repository at this point in the history
refs #3777
  • Loading branch information
Jannis Moßhammer authored and lippserd committed Aug 21, 2013
1 parent c705f5d commit 4810a0d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 66 deletions.
130 changes: 65 additions & 65 deletions library/Icinga/Config/IniEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

namespace Icinga\Config;

use \Zend_Config_Exception;

/**
* Edit the sections and keys of an ini in-place
*/
Expand All @@ -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)
{
Expand All @@ -62,9 +60,9 @@ public function __construct($content)
/**
* 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)
{
Expand All @@ -80,8 +78,8 @@ public function set(array $key, $value, $section = null)
/**
* 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)
{
Expand All @@ -94,9 +92,9 @@ public function resetArrayElement(array $key, $section = null)
/**
* 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)
{
Expand All @@ -119,22 +117,23 @@ public function setArrayElement(array $key, $value, $section = null)
/**
* 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++) {
$l = $this->text[$line];
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)))) {
Expand All @@ -147,8 +146,8 @@ private function getArrayElement(array $key, $section = null)
/**
* 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)
{
Expand All @@ -162,15 +161,15 @@ public function reset(array $key, $section = null)
/**
* 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) {
Expand All @@ -185,7 +184,7 @@ public function setSection($section, $extend = null)
/**
* Remove a section declaration
*
* @param string $section The section name
* @param string $section The section name
*/
public function removeSection($section)
{
Expand All @@ -198,9 +197,9 @@ public function removeSection($section)
/**
* 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)
{
Expand Down Expand Up @@ -260,8 +259,8 @@ private function cleanUpWhitespaces()
/**
* 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)
{
Expand All @@ -271,24 +270,24 @@ private function insertAtLine($lineNr, $toInsert)
/**
* 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;
}

/**
* 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)
{
Expand All @@ -298,14 +297,14 @@ private function getComment($lineContent)
*/
$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)
{
Expand All @@ -315,10 +314,10 @@ private function deleteLine($lineNr)
/**
* 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)
{
Expand All @@ -328,8 +327,9 @@ private function formatKeyValuePair(array $key, $value)
/**
* 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)
{
Expand All @@ -339,14 +339,14 @@ private function formatKey(array $key)
/**
* 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) {
Expand Down Expand Up @@ -374,31 +374,31 @@ private function getSectionEnd($section = null)
/**
* 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;
}

/**
* 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;
}
Expand All @@ -407,9 +407,9 @@ private function isSectionDeclaration($lineContent, $section = null)
/**
* 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)
{
Expand All @@ -426,13 +426,14 @@ private function getSectionLine($section)
/**
* 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) {
Expand All @@ -453,7 +454,7 @@ private function getKeyLine(array $keys, $section = null)
/**
* 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()
{
Expand All @@ -463,9 +464,9 @@ private function getLastLine()
/**
* 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
*/
Expand All @@ -478,12 +479,10 @@ private static function insertIntoArray($array, $pos, $element)
/**
* 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);
Expand All @@ -492,9 +491,10 @@ private function removeFromArray(array $array, $pos)
/**
* 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)
Expand Down
17 changes: 16 additions & 1 deletion test/php/application/forms/Config/GeneralFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ public function testCorrectFieldPopulation()
)
)
);

$form->setResources(
array(
'db' => 'db'
)
);
$form->setConfigDir('/tmp');
$view = new Zend_View();

Expand Down Expand Up @@ -117,7 +121,13 @@ public function testCorrectConditionalIniFieldRendering()
)
);
$form->setConfigDir('/tmp');
$form->setResources(
array(
'db' => 'db'
)
);
$form->create();

$view = new Zend_View();

$this->assertFalse(
Expand Down Expand Up @@ -146,6 +156,11 @@ public function testCorrectConditionalDbFieldRendering()
)
);
$form->setConfigDir('/tmp');
$form->setResources(
array(
'db' => 'db'
)
);
$form->create();
$view = new Zend_View();

Expand Down

0 comments on commit 4810a0d

Please sign in to comment.