Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Modified PhpArray config writer to generate better readable array format. #5259

Closed
wants to merge 13 commits into from
Closed

Modified PhpArray config writer to generate better readable array format. #5259

wants to merge 13 commits into from

Conversation

michaelmoussa
Copy link
Contributor

Saw this Twitter conversation and agreed.

This PR modified Zend\Config\Writer\PhpArray to generate better readable arrays. Specifically, it will (1) indent 4 spaces per level instead of 2 and (2) put the opening array( on the same line as the =>.

Tests pass, and this "shouldn't" affect BC because it's "just" whitespace.


if ($line === '),' || $line === ')') {
$indentLevel--;
} else if (preg_match('/^\s*array \(/', $line)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the line contains array ( as string value? It's probably more safe to rely on token_get_all() here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it's probably better to just build the PHP code up yourself. Doing a var_export() and then parsing it is for once slow and also a little PITA.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DASPRiD I've modified it to build the array by hand. A microtime() based benchmark that I ran locally shows the execution time on the same order of magnitude as the existing simple var_export(...) method.

I've also provided for optional PHP 5.4+ array style generation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the benchmark, that's always a good thing.


foreach ($config as $key => $value) {
$arrayString .= str_repeat(' ', $indentLevel);
$arrayString .= (is_numeric($key) ? $key : "'" . addslashes($key) . "'") . ' => ';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% certain if is_numeric() is correct here, as it would render a string number as integer then as well. Probably better use is_int() and is_float()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to use is_int() (push forthcoming).

The following...

var_dump(array(1.234 => 'hi'));

... truncates the float for the array key, so we'll end up with an int anyway.

@@ -19,8 +24,48 @@ class PhpArray extends AbstractWriter
*/
public function processConfig(array $config)
{
$arrayString = "<?php\n"
. "return " . var_export($config, true) . ";\n";
$array = array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably rename to $arrayStyle or $arraySyntax to make it more clear?

@michaelmoussa
Copy link
Contributor Author

Thanks for the attention and your comments @DASPRiD. If there's anything else I'll take of it over the next few days.

'close' => $this->useBracketArraySyntax ? ']' : ')'
);

return "<?php\n\n" .
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this double linebreak here? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because whoops! :)


foreach ($config as $key => $value) {
$arrayString .= str_repeat(self::INDENT_STRING, $indentLevel);
$arrayString .= (is_int($key) ? $key : "'" . addslashes($key) . "'") . ' => ';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should contain is_float() additionally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #5259 (comment) and The PHP Manual

Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.

is_float(...) will never be true in this scenario.

@michaelmoussa
Copy link
Contributor Author

@DASPRiD or @Maks3w is there anything else you'd like to see done before this can be merged?

@michaelmoussa
Copy link
Contributor Author

Also, the broken Travis build is due to coding standards in files having nothing to do with what I changed. :)

@ghost ghost assigned Maks3w Oct 20, 2013
Maks3w added a commit that referenced this pull request Oct 20, 2013
@Maks3w Maks3w closed this Oct 20, 2013
gianarb pushed a commit to zendframework/zend-config that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants