Fix phpcs errors

This commit is contained in:
Marius Hein 2013-08-20 17:34:47 +02:00
parent d574a26db3
commit df6ccce6f2
2 changed files with 117 additions and 115 deletions

View File

@ -28,7 +28,7 @@
namespace Icinga\Config; namespace Icinga\Config;
use Icinga\Exception\ConfigurationError; use \Zend_Config_Exception;
/** /**
* Edit the sections and keys of an ini in-place * Edit the sections and keys of an ini in-place
@ -52,36 +52,36 @@ class IniEditor
/** /**
* Create a new IniEditor * Create a new IniEditor
* *
* @param $content The content of the ini as string * @param string $content The content of the ini as string
*/ */
public function __construct($content) public function __construct($content)
{ {
$this->text = explode("\n",$content); $this->text = explode("\n", $content);
} }
/** /**
* Set the value of the given key. * Set the value of the given key.
* *
* @param array $key The key to set * @param array $key The key to set
* @param $value The value to set * @param mixed $value The value to set
* @param $section The section to insert to. * @param string $section The section to insert to.
*/ */
public function set(array $key, $value, $section = null) public function set(array $key, $value, $section = null)
{ {
$line = $this->getKeyLine($key,$section); $line = $this->getKeyLine($key, $section);
if ($line === -1) { if ($line === -1) {
$this->insert($key,$value,$section); $this->insert($key, $value, $section);
} else { } else {
$content = $this->formatKeyValuePair($key,$value); $content = $this->formatKeyValuePair($key, $value);
$this->updateLine($line,$content); $this->updateLine($line, $content);
} }
} }
/** /**
* Reset the value of the given array element * Reset the value of the given array element
* *
* @param $key The key of the array value * @param array $key The key of the array value
* @param $section The section of the array. * @param string $section The section of the array.
*/ */
public function resetArrayElement(array $key, $section = null) public function resetArrayElement(array $key, $section = null)
{ {
@ -94,36 +94,35 @@ class IniEditor
/** /**
* Set the value for an array element * Set the value for an array element
* *
* @param array $key The key of the property * @param array $key The key of the property
* @param $value The value of the property * @param mixed $value The value of the property
* @param $section The section to use * @param string $section The section to use
*/ */
public function setArrayElement(array $key, $value, $section = null) public function setArrayElement(array $key, $value, $section = null)
{ {
$line = $this->getArrayElement($key,$section); $line = $this->getArrayElement($key, $section);
if ($line !== -1) { if ($line !== -1) {
if (isset($section)) { if (isset($section)) {
$this->updateLine($line,$this->formatKeyValuePair($key,$value)); $this->updateLine($line, $this->formatKeyValuePair($key, $value));
} else { } else {
$section = $key[0]; $section = $key[0];
unset($key[0]); unset($key[0]);
$this->deleteLine($line); $this->deleteLine($line);
$this->setSection($section); $this->setSection($section);
$this->insert($key,$value,$section); $this->insert($key, $value, $section);
} }
} else { } else {
$this->insert($key,$value,$section); $this->insert($key, $value, $section);
} }
} }
/** /**
* Get the line of an array element * Get the line of an array element
* *
* @param array $key The key of the property. * @param array $key The key of the property.
* @param $value The value * @param string $section The section to use
* @param $section The section to use
* *
* @return The line of the array element. * @return int The line of the array element.
*/ */
private function getArrayElement(array $key, $section = null) private function getArrayElement(array $key, $section = null)
{ {
@ -135,10 +134,10 @@ class IniEditor
if ($this->isSectionDeclaration($l)) { if ($this->isSectionDeclaration($l)) {
return -1; return -1;
} }
if (preg_match('/^\s*'.$formatted.'\[\]\s*=/',$l) === 1) { if (preg_match('/^\s*' . $formatted.'\[\]\s*=/', $l) === 1) {
return $line; return $line;
} }
if ($this->isPropertyDeclaration($l,array_merge($key,array($index)))) { if ($this->isPropertyDeclaration($l, array_merge($key, array($index)))) {
return $line; return $line;
} }
} }
@ -148,12 +147,12 @@ class IniEditor
/** /**
* When it exists, set the key back to null * When it exists, set the key back to null
* *
* @param array $key The key to reset * @param array $key The key to reset
* @param $section The section of the key * @param string $section The section of the key
*/ */
public function reset(array $key, $section = null) public function reset(array $key, $section = null)
{ {
$line = $this->getKeyLine($key,$section); $line = $this->getKeyLine($key, $section);
if ($line === -1) { if ($line === -1) {
return; return;
} }
@ -163,30 +162,30 @@ class IniEditor
/** /**
* Create the section if it does not exist and set the properties * Create the section if it does not exist and set the properties
* *
* @param $section The section name * @param string $section The section name
* @param $extend The section that should be extended by this section * @param string $extend The section that should be extended by this section
*/ */
public function setSection($section, $extend = null) public function setSection($section, $extend = null)
{ {
if (isset($extend)) { if (isset($extend)) {
$decl = '['.$section.' : '.$extend.']'; $decl = '[' . $section . ' : ' . $extend.']';
} else { } else {
$decl = '['.$section.']'; $decl = '[' . $section.']';
} }
$line = $this->getSectionLine($section); $line = $this->getSectionLine($section);
if ($line !== -1) { if ($line !== -1) {
$this->deleteLine($line); $this->deleteLine($line);
$this->insertAtLine($line,$decl); $this->insertAtLine($line, $decl);
} else { } else {
$line = $this->getLastLine(); $line = $this->getLastLine();
$this->insertAtLine($line,$decl); $this->insertAtLine($line, $decl);
} }
} }
/** /**
* Remove a section declaration * Remove a section declaration
* *
* @param $section The section name * @param string $section The section name
*/ */
public function removeSection($section) public function removeSection($section)
{ {
@ -199,15 +198,15 @@ class IniEditor
/** /**
* Insert the key at the end of the corresponding section * Insert the key at the end of the corresponding section
* *
* @param array $key The key to insert * @param array $key The key to insert
* @param $value The value to insert * @param mixed $value The value to insert
* @param array $key The key to insert * @param string $section
*/ */
private function insert(array $key, $value, $section = null) private function insert(array $key, $value, $section = null)
{ {
$line = $this->getSectionEnd($section); $line = $this->getSectionEnd($section);
$content = $this->formatKeyValuePair($key,$value); $content = $this->formatKeyValuePair($key, $value);
$this->insertAtLine($line,$content); $this->insertAtLine($line, $content);
} }
/** /**
@ -218,7 +217,7 @@ class IniEditor
public function getText() public function getText()
{ {
$this->cleanUpWhitespaces(); $this->cleanUpWhitespaces();
return implode("\n",$this->text); return implode("\n", $this->text);
} }
/** /**
@ -235,14 +234,14 @@ class IniEditor
/* /*
* Ignore comments that are glued to the section declaration * Ignore comments that are glued to the section declaration
*/ */
while ($i > 0 && preg_match('/^\s*;/',$line) === 1) { while ($i > 0 && preg_match('/^\s*;/', $line) === 1) {
$i--; $i--;
$line = $this->text[$i]; $line = $this->text[$i];
} }
/* /*
* Remove whitespaces between the sections * Remove whitespaces between the sections
*/ */
while ($i > 0 && preg_match('/^\s*$/',$line) === 1) { while ($i > 0 && preg_match('/^\s*$/', $line) === 1) {
$this->deleteLine($i); $this->deleteLine($i);
$i--; $i--;
$line = $this->text[$i]; $line = $this->text[$i];
@ -251,7 +250,7 @@ class IniEditor
* Add a single whitespace * Add a single whitespace
*/ */
if ($i !== 0) { if ($i !== 0) {
$this->insertAtLine($i + 1,''); $this->insertAtLine($i + 1, '');
} }
} }
} }
@ -260,35 +259,35 @@ class IniEditor
/** /**
* Insert the text at line $lineNr * Insert the text at line $lineNr
* *
* @param $lineNr The line nr the inserted line should have * @param int $lineNr The line nr the inserted line should have
* @param $toInsert The text that will be inserted * @param string $toInsert The text that will be inserted
*/ */
private function insertAtLine($lineNr, $toInsert) private function insertAtLine($lineNr, $toInsert)
{ {
$this->text = IniEditor::insertIntoArray($this->text,$lineNr,$toInsert); $this->text = IniEditor::insertIntoArray($this->text, $lineNr, $toInsert);
} }
/** /**
* Update the line $lineNr * Update the line $lineNr
* *
* @param $lineNr The line number of the target line * @param int $lineNr The line number of the target line
* @param $toInsert The new line content * @param string $content The content to replace
*/ */
private function updateLine($lineNr, $content) private function updateLine($lineNr, $content)
{ {
$comment = $this->getComment($this->text[$lineNr]); $comment = $this->getComment($this->text[$lineNr]);
if (strlen($comment) > 0) { if (strlen($comment) > 0) {
$comment = " ; " . trim($comment); $comment = ' ; ' . trim($comment);
} }
$this->text[$lineNr] = str_pad($content,43) . $comment; $this->text[$lineNr] = str_pad($content, 43) . $comment;
} }
/** /**
* Get the comment from the given line * Get the comment from the given line
* *
* @param $lineContent The content of the line * @param string $lineContent The content of the line
* *
* @return string The extracted comment * @return string The extracted comment
*/ */
private function getComment($lineContent) private function getComment($lineContent)
{ {
@ -296,49 +295,52 @@ class IniEditor
* Remove all content in double quotes that is not behind a semicolon, recognizing * Remove all content in double quotes that is not behind a semicolon, recognizing
* escaped double quotes inside the string * escaped double quotes inside the string
*/ */
$cleaned = preg_replace('/^[^;"]*"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"/s','',$lineContent); $cleaned = preg_replace('/^[^;"]*"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"/s', '', $lineContent);
$matches = mb_split(';',$cleaned,2); $matches = mb_split(';', $cleaned, 2);
return array_key_exists(1,$matches) ? $matches[1] : ""; return array_key_exists(1, $matches) ? $matches[1] : '';
} }
/** /**
* Delete the line $lineNr * Delete the line $lineNr
* *
* @param $lineNr The lineNr starting at 0 * @param int $lineNr The lineNr starting at 0
*/ */
private function deleteLine($lineNr) private function deleteLine($lineNr)
{ {
$this->text = $this->removeFromArray($this->text,$lineNr); $this->text = $this->removeFromArray($this->text, $lineNr);
} }
/** /**
* Format a key-value pair to an INI file-entry * Format a key-value pair to an INI file-entry
* *
* @param array $key The key * @param array $key The key
* @param $value The value * @param mixed $value The value
* *
* @return string The formatted key-value pair * @return string The formatted key-value pair
*/ */
private function formatKeyValuePair(array $key, $value) private function formatKeyValuePair(array $key, $value)
{ {
return str_pad($this->formatKey($key),19) . " = ".$this->formatValue($value); return str_pad($this->formatKey($key), 19) . ' = ' . $this->formatValue($value);
} }
/** /**
* Format a key to an INI key * Format a key to an INI key
*
* @param array $key
* @return string
*/ */
private function formatKey(array $key) private function formatKey(array $key)
{ {
return implode($this->nestSeparator,$key); return implode($this->nestSeparator, $key);
} }
/** /**
* Get the first line after the given $section * Get the first line after the given $section
* *
* @param $section The name of the section * @param string $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) private function getSectionEnd($section = null)
{ {
@ -353,11 +355,11 @@ class IniEditor
* ignore all comments 'glued' to the next section, to allow section * ignore all comments 'glued' to the next section, to allow section
* comments in front of sections * comments in front of sections
*/ */
while ($i > 0 && preg_match('/^\s*;/',$this->text[$i - 1]) === 1) { while ($i > 0 && preg_match('/^\s*;/', $this->text[$i - 1]) === 1) {
$i--; $i--;
} }
return $i; return $i;
} elseif ( $this->isSectionDeclaration($line,$section) ) { } elseif ($this->isSectionDeclaration($line, $section)) {
$started = true; $started = true;
} }
$i++; $i++;
@ -371,48 +373,48 @@ class IniEditor
/** /**
* Check if the line contains the property declaration for a key * Check if the line contains the property declaration for a key
* *
* @param $lineContent The content of the line * @param string $lineContent The content of the line
* @param array $key The key this declaration is supposed to have * @param array $key The key this declaration is supposed to have
* *
* @return boolean True, when the lineContent is a property declaration * @return bool True, when the lineContent is a property declaration
*/ */
private function isPropertyDeclaration($lineContent, array $key) private function isPropertyDeclaration($lineContent, array $key)
{ {
return preg_match( return preg_match(
'/^\s*' . $this->formatKey($key) .'\s*=\s*/' '/^\s*' . $this->formatKey($key) .'\s*=\s*/',
,$lineContent $lineContent
) === 1; ) === 1;
} }
/** /**
* Check if the given line contains a section declaration * Check if the given line contains a section declaration
* *
* @param $lineContent The content of the line * @param string $lineContent The content of the line
* @param string $section The optional section name that will be assumed * @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) private function isSectionDeclaration($lineContent, $section = null)
{ {
if (isset($section)) { if (isset($section)) {
return preg_match('/^\s*\[\s*'.$section.'\s*[\]:]/',$lineContent) === 1; return preg_match('/^\s*\[\s*'.$section.'\s*[\]:]/', $lineContent) === 1;
} else { } else {
return preg_match('/^\s*\[/',$lineContent) === 1; return preg_match('/^\s*\[/', $lineContent) === 1;
} }
} }
/** /**
* Get the line where the section begins * Get the line where the section begins
* *
* @param $section The section * @param string $section The section
* *
* @return int The line number * @return int The line number
*/ */
private function getSectionLine($section) private function getSectionLine($section)
{ {
$i = 0; $i = 0;
foreach ($this->text as $line) { foreach ($this->text as $line) {
if ($this->isSectionDeclaration($line,$section)) { if ($this->isSectionDeclaration($line, $section)) {
return $i; return $i;
} }
$i++; $i++;
@ -423,24 +425,23 @@ class IniEditor
/** /**
* Get the line number where the given key occurs * Get the line number where the given key occurs
* *
* @param array $keys The key and its parents * @param array $keys The key and its parents
* @param $section The section of the key * @param string $section The section of the key
* *
* @return int The line number * @return int The line number
*/ */
private function getKeyLine(array $keys, $section = null) private function getKeyLine(array $keys, $section = null)
{ {
$key = implode($this->nestSeparator,$keys);
$inSection = isset($section) ? false : true; $inSection = isset($section) ? false : true;
$i = 0; $i = 0;
foreach ($this->text as $line) { foreach ($this->text as $line) {
if ($inSection && $this->isSectionDeclaration($line)) { if ($inSection && $this->isSectionDeclaration($line)) {
return -1; return -1;
} }
if ($inSection && $this->isPropertyDeclaration($line,$keys)) { if ($inSection && $this->isPropertyDeclaration($line, $keys)) {
return $i; return $i;
} }
if (!$inSection && $this->isSectionDeclaration($line,$section)) { if (!$inSection && $this->isSectionDeclaration($line, $section)) {
$inSection = true; $inSection = true;
} }
$i++; $i++;
@ -451,7 +452,7 @@ class IniEditor
/** /**
* Get the last line number occurring in the text * Get the last line number occurring in the text
* *
* @return The line number of the last line * @return int The line number of the last line
*/ */
private function getLastLine() private function getLastLine()
{ {
@ -461,9 +462,9 @@ class IniEditor
/** /**
* Insert a new element into a specific position of an array * Insert a new element into a specific position of an array
* *
* @param $array The array to use * @param array $array The array to use
* @param $pos The target position * @param int $pos The target position
* @param $element The element to insert * @param mixed $element The element to insert
* *
* @return array The changed array * @return array The changed array
*/ */
@ -476,10 +477,12 @@ class IniEditor
/** /**
* Remove an element from an array * Remove an element from an array
* *
* @param $array The array to use * @param array $array The array to use
* @param $pos The position to remove * @param mixed $pos The position to remove
*
* @return array
*/ */
private function removeFromArray($array, $pos) private function removeFromArray(array $array, $pos)
{ {
unset($array[$pos]); unset($array[$pos]);
return array_values($array); return array_values($array);
@ -488,10 +491,9 @@ class IniEditor
/** /**
* Prepare a value for INe * Prepare a value for INe
* *
* @param $value The value of the string * @param mixed $value The value of the string
*
* @return string The formatted value
* *
* @return string The formatted value
* @throws Zend_Config_Exception * @throws Zend_Config_Exception
*/ */
private function formatValue($value) private function formatValue($value)
@ -503,8 +505,7 @@ class IniEditor
} elseif (strpos($value, '"') === false) { } elseif (strpos($value, '"') === false) {
return '"' . $value . '"'; return '"' . $value . '"';
} else { } else {
return '"' . str_replace('"','\"',$value) . '"'; return '"' . str_replace('"', '\"', $value) . '"';
} }
} }
} }

View File

@ -28,14 +28,16 @@
namespace Icinga\Config; namespace Icinga\Config;
use Zend_Config; use \Zend_Config;
use Zend_Config_Ini; use \Zend_Config_Ini;
use \Zend_Config_Writer_FileAbstract;
use \Icinga\Config\IniEditor;
/** /**
* A ini file adapter that respects the file structure and the comments of already * A ini file adapter that respects the file structure and the comments of already
* existing ini files * existing ini files
*/ */
class PreservingIniWriter extends \Zend_Config_Writer_FileAbstract class PreservingIniWriter extends Zend_Config_Writer_FileAbstract
{ {
/** /**
* Render the Zend_Config into a config file string * Render the Zend_Config into a config file string
@ -47,7 +49,7 @@ class PreservingIniWriter extends \Zend_Config_Writer_FileAbstract
$oldconfig = new Zend_Config_Ini($this->_filename); $oldconfig = new Zend_Config_Ini($this->_filename);
$newconfig = $this->_config; $newconfig = $this->_config;
$editor = new IniEditor(file_get_contents($this->_filename)); $editor = new IniEditor(file_get_contents($this->_filename));
$this->diffConfigs($oldconfig,$newconfig,$editor); $this->diffConfigs($oldconfig, $newconfig, $editor);
return $editor->getText(); return $editor->getText();
} }
@ -65,8 +67,8 @@ class PreservingIniWriter extends \Zend_Config_Writer_FileAbstract
IniEditor $editor, IniEditor $editor,
array $parents = array() array $parents = array()
) { ) {
$this->diffPropertyUpdates($oldconfig,$newconfig,$editor,$parents); $this->diffPropertyUpdates($oldconfig, $newconfig, $editor, $parents);
$this->diffPropertyDeletions($oldconfig,$newconfig,$editor,$parents); $this->diffPropertyDeletions($oldconfig, $newconfig, $editor, $parents);
} }
/** /**
@ -94,9 +96,9 @@ class PreservingIniWriter extends \Zend_Config_Writer_FileAbstract
*/ */
foreach ($newconfig as $key => $value) { foreach ($newconfig as $key => $value) {
$oldvalue = $oldconfig->get($key); $oldvalue = $oldconfig->get($key);
$nextParents = array_merge($parents,array($key)); $nextParents = array_merge($parents, array($key));
$keyIdentifier = empty($parents) ? $keyIdentifier = empty($parents) ?
array($key) : array_slice($nextParents,1,null,true); array($key) : array_slice($nextParents, 1, null, true);
if ($value instanceof Zend_Config) { if ($value instanceof Zend_Config) {
/* /*
@ -107,22 +109,22 @@ class PreservingIniWriter extends \Zend_Config_Writer_FileAbstract
* Update the section declaration * Update the section declaration
*/ */
$extends = $newconfig->getExtends(); $extends = $newconfig->getExtends();
$extend = array_key_exists($key,$extends) ? $extend = array_key_exists($key, $extends) ?
$extends[$key] : null; $extends[$key] : null;
$editor->setSection($key,$extend); $editor->setSection($key, $extend);
} }
if (!isset($oldvalue)) { if (!isset($oldvalue)) {
$oldvalue = new Zend_Config(array()); $oldvalue = new Zend_Config(array());
} }
$this->diffConfigs($oldvalue,$value,$editor,$nextParents); $this->diffConfigs($oldvalue, $value, $editor, $nextParents);
} else { } else {
/* /*
* The value is a plain value, use the editor to set it * The value is a plain value, use the editor to set it
*/ */
if (is_numeric($key)) { if (is_numeric($key)) {
$editor->setArrayElement($keyIdentifier,$value,$section); $editor->setArrayElement($keyIdentifier, $value, $section);
} else { } else {
$editor->set($keyIdentifier,$value,$section); $editor->set($keyIdentifier, $value, $section);
} }
} }
} }
@ -153,17 +155,17 @@ class PreservingIniWriter extends \Zend_Config_Writer_FileAbstract
* deleted properties * deleted properties
*/ */
foreach ($oldconfig as $key => $value) { foreach ($oldconfig as $key => $value) {
$nextParents = array_merge($parents,array($key)); $nextParents = array_merge($parents, array($key));
$newvalue = $newconfig->get($key); $newvalue = $newconfig->get($key);
$keyIdentifier = empty($parents) ? $keyIdentifier = empty($parents) ?
array($key) : array_slice($nextParents,1,null,true); array($key) : array_slice($nextParents, 1, null, true);
if (!isset($newvalue)) { if (!isset($newvalue)) {
if ($value instanceof Zend_Config) { if ($value instanceof Zend_Config) {
/* /*
* The deleted value is a nested Zend_Config, handle it recursively * The deleted value is a nested Zend_Config, handle it recursively
*/ */
$this->diffConfigs($value,new Zend_Config(array()),$editor,$nextParents); $this->diffConfigs($value, new Zend_Config(array()), $editor, $nextParents);
if (!isset($section)) { if (!isset($section)) {
$editor->removeSection($key); $editor->removeSection($key);
} }
@ -172,13 +174,12 @@ class PreservingIniWriter extends \Zend_Config_Writer_FileAbstract
* The deleted value is a plain value, use the editor to delete it * The deleted value is a plain value, use the editor to delete it
*/ */
if (is_numeric($key)) { if (is_numeric($key)) {
$editor->resetArrayElement($keyIdentifier,$section); $editor->resetArrayElement($keyIdentifier, $section);
} else { } else {
$editor->reset($keyIdentifier,$section); $editor->reset($keyIdentifier, $section);
} }
} }
} }
} }
} }
} }