Commit b98089d5 authored by Taddeus Kroes's avatar Taddeus Kroes

Updated code style to C-like standards

parent 34788e78
......@@ -170,7 +170,7 @@ class Autoloader extends Base {
private function strip_root_namespace($classname) {
$begin = substr($classname, 0, strlen($this->root_namespace));
if( $begin == $this->root_namespace )
if ($begin == $this->root_namespace)
$classname = substr($classname, strlen($this->root_namespace));
return $classname;
......@@ -189,7 +189,7 @@ class Autoloader extends Base {
$dirs = array_map('self::classname_to_filename', $namespaces);
$path = $this->root_directory;
if( count($dirs) > 1 )
if (count($dirs) > 1)
$path .= implode('/', array_slice($dirs, 0, count($dirs) - 1)).'/';
$path .= end($dirs).'.php';
......@@ -211,8 +211,8 @@ class Autoloader extends Base {
$classname = $this->strip_root_namespace($classname);
$path = $this->create_path($classname);
if( !file_exists($path) ) {
if( $throw || ($throw === null && $this->throw_errors) )
if (!file_exists($path)) {
if ($throw || ($throw === null && $this->throw_errors))
throw new FileNotFoundError($path);
return false;
......
......@@ -99,7 +99,7 @@ class FileNotFoundError extends \RuntimeException {
function asprintf($format, array $params) {
return preg_replace_callback(
'/%\(([a-z-_ ]*)\)/i',
function ($matches) use ($params) {
function($matches) use ($params) {
return (string)$params[$matches[1]];
},
$format
......
......@@ -77,11 +77,11 @@ class Collection extends Base {
* @throws \InvalidArgumentException If the added index already exists, and is non-numeric.
*/
function insert($item, $index) {
if( isset($this->items[$index]) ) {
if( !is_int($index) )
if (isset($this->items[$index])) {
if (!is_int($index))
throw new \InvalidArgumentException(sprintf('Index "%s" already exists in this collection.', $index));
for( $i = count($this->items) - 1; $i >= $index; $i--)
for ($i = count($this->items) - 1; $i >= $index; $i--)
$this->items[$i + 1] = $this->items[$i];
}
......@@ -104,7 +104,7 @@ class Collection extends Base {
* @throws \OutOfBoundsException if the collection is empty.
*/
function first() {
if( !$this->count() )
if (!$this->count())
throw new \OutOfBoundsException(sprintf('Cannot get first item: collection is empty.'));
return $this->items[0];
......@@ -117,7 +117,7 @@ class Collection extends Base {
* @throws \OutOfBoundsException if the collection is empty.
*/
function last() {
if( !$this->count() )
if (!$this->count())
throw new \OutOfBoundsException(sprintf('Cannot get last item: collection is empty.'));
return end($this->items);
......@@ -179,7 +179,7 @@ class Collection extends Base {
* @return Collection A collection with the new item set.
*/
private function set_items(array $items, $clone=true) {
if( $clone )
if ($clone)
return new self($items);
$this->items = $items;
......@@ -225,14 +225,14 @@ class Collection extends Base {
*/
function find(array $conditions, $clone=true) {
return $this->filter(function($item) use ($conditions) {
if( is_object($item) ) {
if (is_object($item)) {
// Object, match property values
foreach( $conditions as $property => $value )
if( $item->{$property} != $value )
return false;
} elseif( is_array($item) ) {
} elseif (is_array($item)) {
// Array item, match array values
foreach( $conditions as $property => $value )
foreach ($conditions as $property => $value)
if( $item[$property] != $value )
return false;
} else {
......@@ -302,7 +302,7 @@ class Collection extends Base {
function map_method($method_name, array $args=array(), $clone=true) {
$items = array();
foreach( $this->items as $item )
foreach ($this->items as $item)
$items[] = call_user_func_array(array($item, $method_name), $args);
return $this->set_items($items);
......
......@@ -40,7 +40,7 @@ class Logger extends Base {
}
function set_dump_format($format) {
if( !in_array($format, self::$allowed_dump_formats) )
if (!in_array($format, self::$allowed_dump_formats))
throw new \InvalidArgumentException(sprintf('', $format));
$this->dump_format = $format;
......@@ -63,16 +63,16 @@ class Logger extends Base {
}
function set_level($level) {
if( is_string($level) ) {
if (is_string($level)) {
$level = strtoupper($level);
if( !defined('self::'.$level) )
if (!defined('self::'.$level))
throw new \InvalidArgumentException(sprintf('Invalid debug level %s.', $level));
$level = constant('self::'.$level);
}
if( $level < self::CRITICAL || $level > self::DEBUG )
if ($level < self::CRITICAL || $level > self::DEBUG)
throw new \InvalidArgumentException(sprintf('Invalid debug level %d.', $level));
$this->level = $level;
......@@ -111,7 +111,7 @@ class Logger extends Base {
}
private function process($message, $level) {
if( $level <= $this->level )
if ($level <= $this->level)
$this->output[] = array($message, $level);
}
......@@ -119,18 +119,18 @@ class Logger extends Base {
$logger = $this;
$output = '';
foreach( $this->output as $i => $tuple ) {
foreach ($this->output as $i => $tuple) {
list($message, $level) = $tuple;
$i && $output .= "\n";
$output .= preg_replace_callback(
'/%\(([a-z-_ ]*)\)/i',
function ($matches) use ($logger, $message, $level) {
function($matches) use ($logger, $message, $level) {
$name = $matches[1];
if( $name == 'message' )
if ($name == 'message')
return $message;
if( $name == 'level' )
if ($name == 'level')
return Logger::$level_names[$level];
return $logger->get_formatted_property($matches[1]);
......@@ -143,7 +143,7 @@ class Logger extends Base {
}
function dump($file_prefix='log') {
switch( $this->dump_format ) {
switch ($this->dump_format) {
case 'none':
return;
case 'plain':
......@@ -163,7 +163,7 @@ class Logger extends Base {
}
function save($path) {
if( $this->log_directory && !is_dir($this->log_directory) )
if ($this->log_directory && !is_dir($this->log_directory))
mkdir($this->log_directory, 0777, true);
file_put_contents($this->log_directory . $path, $this->dumps());
......@@ -184,7 +184,7 @@ class Logger extends Base {
}
function get_formatted_property($property) {
if( isset($this->properties[$property]) )
if (isset($this->properties[$property]))
return $this->properties[$property];
switch( $property ) {
......
......@@ -183,7 +183,7 @@ class Node extends Base {
* @param Node &$child The node to remove.
*/
function remove_child(Node &$child) {
foreach( $this->children as $i => $node )
foreach ($this->children as $i => $node)
$node->is($child) && array_splice($this->children, $i, 1);
}
......@@ -194,12 +194,12 @@ class Node extends Base {
* @return Node This node.
*/
function remove() {
if( $this->is_root() )
if ($this->is_root())
throw new \RuntimeException('Cannot remove the root node of a tree.');
$this->parent_node->remove_child($this);
foreach( $this->children as $child )
foreach ($this->children as $child)
$child->set_parent(null);
return $this;
......@@ -215,7 +215,7 @@ class Node extends Base {
* @return Node This node.
*/
function set_parent($parent) {
if( $this->parent_node !== null )
if ($this->parent_node !== null)
$this->parent_node->remove_child($this);
$this->parent_node = &$parent;
......@@ -231,8 +231,8 @@ class Node extends Base {
* @return Node This node.
*/
function set($name, $value=null) {
if( is_array($name) ) {
foreach( $name as $var => $val )
if (is_array($name)) {
foreach ($name as $var => $val)
$this->variables[$var] = $val;
} else {
$this->variables[$name] = $value;
......@@ -249,7 +249,7 @@ class Node extends Base {
*/
function get($name) {
// Variable inside this node?
if( isset($this->variables[$name]) )
if (isset($this->variables[$name]))
return $this->variables[$name];
// Variable in one of ancestors?
......@@ -311,8 +311,8 @@ class Node extends Base {
$copy = new self($this->name, $this->parent_node, $this->id);
$copy->set($this->variables);
foreach( $this->children as $child ) {
if( $deep ) {
foreach ($this->children as $child) {
if ($deep) {
$child_copy = $child->copy(true);
$copy->add_child($child_copy);
} else {
......
......@@ -69,7 +69,7 @@ class Router extends Base {
* @param array $routes An initial list of routes to set.
*/
function __construct(array $routes=array()) {
foreach( $routes as $pattern => $handler )
foreach ($routes as $pattern => $handler)
$this->add_route($pattern, $handler);
}
......@@ -88,10 +88,10 @@ class Router extends Base {
* @throws \InvalidArgumentException If $handler is not callable.
*/
function add_route($pattern, $handler) {
if( !is_callable($handler) )
if (!is_callable($handler))
throw new \InvalidArgumentException(sprintf('Handler for patterns "%s" is not callable.', $pattern));
$this->routes[self::DELIMITER.'^'.$pattern.'$'.self::DELIMITER] = $handler;
$this->routes[self::DELIMITER . '^' . $pattern . '$' . self::DELIMITER] = $handler;
}
/**
......@@ -108,12 +108,12 @@ class Router extends Base {
* corresponding handler function otherwise.
*/
function call_handler($url) {
foreach( $this->routes as $pattern => $handler ) {
if( preg_match($pattern, $url, $matches) ) {
foreach ($this->routes as $pattern => $handler) {
if (preg_match($pattern, $url, $matches)) {
array_shift($matches);
$result = call_user_func_array($handler, $matches);
if( $result !== false )
if ($result !== false)
return $result;
}
}
......
......@@ -146,10 +146,10 @@ class Template extends Node {
$look_in = count(self::$include_path) ? self::$include_path : array('.');
$found = false;
foreach( $look_in as $root ) {
foreach ($look_in as $root) {
$path = $root.$filename;
if( file_exists($path) ) {
if (file_exists($path)) {
$this->path = $path;
$this->file_content = file_get_contents($path);
$found = true;
......@@ -157,7 +157,7 @@ class Template extends Node {
}
}
if( !$found ) {
if (!$found) {
throw new \RuntimeException(
sprintf("Could not find template file \"%s\", looked in folders:\n%s",
$filename, implode("\n", $look_in))
......@@ -187,25 +187,25 @@ class Template extends Node {
$after = $this->file_content;
$line_count = 0;
while( preg_match('/(.*?)\{([^}]+)}(.*)/s', $after, $matches) ) {
while (preg_match('/(.*?)\{([^}]+)}(.*)/s', $after, $matches)) {
list($before, $brackets_content, $after) = array_slice($matches, 1);
$line_count += substr_count($before, "\n");
// Everything before the new block belongs to its parent
$html = $current->add('html')->set('content', $before);
if( $brackets_content == 'end' ) {
if ($brackets_content == 'end') {
// {end} encountered, go one level up in the tree
if( $current->is_root() )
if ($current->is_root())
throw new ParseError($this, 'unexpected {end}', $line_count + 1);
$current = $current->get_parent();
} elseif( substr($brackets_content, 0, 6) == 'block:' ) {
} elseif(substr($brackets_content, 0, 6) == 'block:') {
// {block:...} encountered
$block_name = substr($brackets_content, 6);
// Go one level deeper into the tree
$current = $current->add('block')->set('name', $block_name);
} elseif( strpos($brackets_content, "\n") !== false ) {
} elseif (strpos($brackets_content, "\n") !== false) {
// Bracket content contains newlines, so it is probably JavaScript or CSS
$html->set('content', $before . '{' . $brackets_content . '}');
} else {
......@@ -216,7 +216,7 @@ class Template extends Node {
$line_count += substr_count($after, "\n");
if( $current !== $root )
if ($current !== $root)
throw new ParseError($this, 'missing {end}', $line_count + 1);
// Add the last remaining content to the root node
......@@ -246,15 +246,15 @@ class Template extends Node {
private static function render_block(Node $block, Node $data) {
$html = '';
foreach( $block->get_children() as $child ) {
switch( $child->get_name() ) {
foreach ($block->get_children() as $child) {
switch ($child->get_name()) {
case 'html':
$html .= $child->get('content');
break;
case 'block':
$block_name = $child->get('name');
foreach( $data->find($block_name) as $child_data )
foreach ($data->find($block_name) as $child_data)
$html .= self::render_block($child, $child_data);
break;
......@@ -284,11 +284,11 @@ class Template extends Node {
$variable = $matches[3];
$value = $data->get($variable);
if( count($matches) == 5 ) {
if (count($matches) == 5) {
// $<name>.<name>
$attribute = $matches[4];
if( $value === null ) {
if ($value === null) {
throw new \UnexpectedValueException(
sprintf('Cannot get attribute "%s.%s": value is NULL', $variable, $attribute)
);
......@@ -300,20 +300,20 @@ class Template extends Node {
);
};
if( is_array($value) ) {
if (is_array($value)) {
isset($value[$attribute]) || $attr_error('no such key', '\OutOfBoundsException');
$value = $value[$attribute];
} elseif( is_object($value) ) {
} elseif (is_object($value)) {
isset($value->$attribute) || $attr_error('no such attribute');
$value = $value->$attribute;
} else {
$attr_error('variable is no array or object');
}
} elseif( count($matches) == 6 ) {
} elseif (count($matches) == 6) {
// $<name>.<name>()
$method = $matches[4];
if( $value === null ) {
if ($value === null) {
throw new \UnexpectedValueException(
sprintf('Cannot call method "%s.%s()": object is NULL', $variable, $method)
);
......@@ -325,7 +325,7 @@ class Template extends Node {
);
};
if( is_object($value) ) {
if (is_object($value)) {
method_exists($value, $method) || $method_error('no such method');
$value = $value->$method();
} else {
......@@ -334,7 +334,7 @@ class Template extends Node {
}
// Escape value
if( is_string($value) && !$noescape_sign )
if (is_string($value) && !$noescape_sign)
$value = self::escape_variable_value($value);
return $before . $value;
......@@ -363,10 +363,10 @@ class Template extends Node {
* @return string The evaluation of the condition.
*/
private static function evaluate_condition(array $matches, Node $data) {
if( self::evaluate_expression($matches[1], $data, false) ) {
if (self::evaluate_expression($matches[1], $data, false)) {
// Condition evaluates to true: return 'if' evaluation
return self::evaluate_expression($matches[2], $data, false);
} elseif( count($matches) == 4 ) {
} elseif (count($matches) == 4) {
// <nested_exp>?<nested_exp>:<nested_exp>
return self::evaluate_expression($matches[3], $data, false);
}
......@@ -390,7 +390,7 @@ class Template extends Node {
$function = $matches[1];
$parameter = $matches[2];
if( !is_callable($function) ) {
if (!is_callable($function)) {
throw new \BadFunctionCallException(
sprintf('Cannot call function "%s": function is not callable', $function)
);
......@@ -413,7 +413,7 @@ class Template extends Node {
* original constant name otherwise.
*/
private static function evaluate_constant($constant, $root_level) {
if( defined($constant) )
if (defined($constant))
return constant($constant);
return $root_level ? '{' . $constant . '}' : $constant;
......@@ -434,24 +434,24 @@ class Template extends Node {
* original string enclosed in curly brackets otherwise.
*/
private static function evaluate_expression($expression, Node $data, $root_level=true) {
if( $expression ) {
if ($expression) {
$name = '[a-zA-Z0-9-_]+';
$function = "$name(?:::$name)?";
if( preg_match("/^([^?]*?)\s*\?([^:]*)(?::(.*))?$/", $expression, $matches) ) {
if (preg_match("/^([^?]*?)\s*\?([^:]*)(?::(.*))?$/", $expression, $matches)) {
// <nested_exp>?<nested_exp> | <nested_exp>?<nested_exp>:<nested_exp>
return self::evaluate_condition($matches, $data);
} elseif( preg_match("/^(.*?)\\$(\\$?)($name)(?:\.($name)(\(\))?)?$/", $expression, $matches) ) {
} elseif (preg_match("/^(.*?)\\$(\\$?)($name)(?:\.($name)(\(\))?)?$/", $expression, $matches)) {
// $<name> | $<name>.<name> | $<name>.<name>()
// | $$<name> | $$<name>.<name> | $$<name>.<name>()
return self::evaluate_variable($matches, $data);
} elseif( preg_match("/^($function)\((.+?)\)?$/", $expression, $matches) ) {
} elseif (preg_match("/^($function)\((.+?)\)?$/", $expression, $matches)) {
// <function>(<nested_exp>)
return self::evaluate_function($matches, $data);
} elseif( preg_match("/^([A-Z0-9_]+)$/", $expression, $matches) ) {
} elseif (preg_match("/^([A-Z0-9_]+)$/", $expression, $matches)) {
// <constant>
return self::evaluate_constant($expression, $root_level);
} elseif( ($split_at = strpos($expression, '||', 1)) !== false ) {
} elseif (($split_at = strpos($expression, '||', 1)) !== false) {
// <nested_exp>||<nested_exp>
try {
return self::evaluate_expression(substr($expression, 0, $split_at), $data, false);
......@@ -490,10 +490,10 @@ class Template extends Node {
* @throws FileNotFoundError If the path does not exist.
*/
static function add_root($path) {
if( $path[strlen($path) - 1] != '/' )
if ($path[strlen($path) - 1] != '/')
$path .= '/';
if( !is_dir($path) )
if (!is_dir($path))
throw new FileNotFoundError($path, true);
self::$include_path[] = $path;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment