diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-04-10 14:19:56 +0200 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-04-10 14:19:56 +0200 |
commit | caff1023ea72bb2ea94130e18a2a6e2ccf819e5f (patch) | |
tree | 186d494c2aea5dea7255d3584ef5d595fc6e6194 /lib/public/AppFramework/Db | |
parent | edf8ce32cffdb920e8171207b342abbd7f1fbe73 (diff) | |
download | nextcloud-server-caff1023ea72bb2ea94130e18a2a6e2ccf819e5f.tar.gz nextcloud-server-caff1023ea72bb2ea94130e18a2a6e2ccf819e5f.zip |
Format control structures, classes, methods and function
To continue this formatting madness, here's a tiny patch that adds
unified formatting for control structures like if and loops as well as
classes, their methods and anonymous functions. This basically forces
the constructs to start on the same line. This is not exactly what PSR2
wants, but I think we can have a few exceptions with "our" style. The
starting of braces on the same line is pracrically standard for our
code.
This also removes and empty lines from method/function bodies at the
beginning and end.
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/public/AppFramework/Db')
-rw-r--r-- | lib/public/AppFramework/Db/DoesNotExistException.php | 1 | ||||
-rw-r--r-- | lib/public/AppFramework/Db/Entity.php | 26 | ||||
-rw-r--r-- | lib/public/AppFramework/Db/IMapperException.php | 3 | ||||
-rw-r--r-- | lib/public/AppFramework/Db/Mapper.php | 25 | ||||
-rw-r--r-- | lib/public/AppFramework/Db/MultipleObjectsReturnedException.php | 1 | ||||
-rw-r--r-- | lib/public/AppFramework/Db/QBMapper.php | 23 |
6 files changed, 34 insertions, 45 deletions
diff --git a/lib/public/AppFramework/Db/DoesNotExistException.php b/lib/public/AppFramework/Db/DoesNotExistException.php index 36cab24db76..3605bdce274 100644 --- a/lib/public/AppFramework/Db/DoesNotExistException.php +++ b/lib/public/AppFramework/Db/DoesNotExistException.php @@ -42,5 +42,4 @@ class DoesNotExistException extends \Exception implements IMapperException { public function __construct($msg) { parent::__construct($msg); } - } diff --git a/lib/public/AppFramework/Db/Entity.php b/lib/public/AppFramework/Db/Entity.php index 6221f96b375..954b8787c4c 100644 --- a/lib/public/AppFramework/Db/Entity.php +++ b/lib/public/AppFramework/Db/Entity.php @@ -34,7 +34,6 @@ use function substr; * @since 7.0.0 */ abstract class Entity { - public $id; private $_updatedFields = []; @@ -51,7 +50,7 @@ abstract class Entity { public static function fromParams(array $params) { $instance = new static(); - foreach($params as $key => $value) { + foreach ($params as $key => $value) { $method = 'set' . ucfirst($key); $instance->$method($value); } @@ -68,7 +67,7 @@ abstract class Entity { public static function fromRow(array $row) { $instance = new static(); - foreach($row as $key => $value){ + foreach ($row as $key => $value) { $prop = ucfirst($instance->columnToProperty($key)); $setter = 'set' . $prop; $instance->$setter($value); @@ -103,18 +102,17 @@ abstract class Entity { */ protected function setter($name, $args) { // setters should only work for existing attributes - if(property_exists($this, $name)){ - if($this->$name === $args[0]) { + if (property_exists($this, $name)) { + if ($this->$name === $args[0]) { return; } $this->markFieldUpdated($name); // if type definition exists, cast to correct type - if($args[0] !== null && array_key_exists($name, $this->_fieldTypes)) { + if ($args[0] !== null && array_key_exists($name, $this->_fieldTypes)) { settype($args[0], $this->_fieldTypes[$name]); } $this->$name = $args[0]; - } else { throw new \BadFunctionCallException($name . ' is not a valid attribute'); @@ -127,7 +125,7 @@ abstract class Entity { */ protected function getter($name) { // getters should only work for existing attributes - if(property_exists($this, $name)){ + if (property_exists($this, $name)) { return $this->$name; } else { throw new \BadFunctionCallException($name . @@ -154,7 +152,6 @@ abstract class Entity { throw new \BadFunctionCallException($methodName . ' does not exist'); } - } /** @@ -190,8 +187,8 @@ abstract class Entity { $parts = explode('_', $columnName); $property = null; - foreach($parts as $part){ - if($property === null){ + foreach ($parts as $part) { + if ($property === null) { $property = $part; } else { $property .= ucfirst($part); @@ -212,8 +209,8 @@ abstract class Entity { $parts = preg_split('/(?=[A-Z])/', $property); $column = null; - foreach($parts as $part){ - if($column === null){ + foreach ($parts as $part) { + if ($column === null) { $column = $part; } else { $column .= '_' . lcfirst($part); @@ -254,7 +251,7 @@ abstract class Entity { */ public function slugify($attributeName) { // toSlug should only work for existing attributes - if(property_exists($this, $attributeName)){ + if (property_exists($this, $attributeName)) { $value = $this->$attributeName; // replace everything except alphanumeric with a single '-' $value = preg_replace('/[^A-Za-z0-9]+/', '-', $value); @@ -266,5 +263,4 @@ abstract class Entity { ' is not a valid attribute'); } } - } diff --git a/lib/public/AppFramework/Db/IMapperException.php b/lib/public/AppFramework/Db/IMapperException.php index f8a1d96aa93..04f7e380f55 100644 --- a/lib/public/AppFramework/Db/IMapperException.php +++ b/lib/public/AppFramework/Db/IMapperException.php @@ -29,4 +29,5 @@ namespace OCP\AppFramework\Db; /** * @since 16.0.0 */ -interface IMapperException {} +interface IMapperException { +} diff --git a/lib/public/AppFramework/Db/Mapper.php b/lib/public/AppFramework/Db/Mapper.php index 289ac1d684d..b1deaee5412 100644 --- a/lib/public/AppFramework/Db/Mapper.php +++ b/lib/public/AppFramework/Db/Mapper.php @@ -36,7 +36,6 @@ use OCP\IDBConnection; * @deprecated 14.0.0 Move over to QBMapper */ abstract class Mapper { - protected $tableName; protected $entityClass; protected $db; @@ -55,7 +54,7 @@ abstract class Mapper { // if not given set the entity name to the class without the mapper part // cache it here for later use since reflection is slow - if($entityClass === null) { + if ($entityClass === null) { $this->entityClass = str_replace('Mapper', '', get_class($this)); } else { $this->entityClass = $entityClass; @@ -105,7 +104,7 @@ abstract class Mapper { // build the fields $i = 0; - foreach($properties as $property => $updated) { + foreach ($properties as $property => $updated) { $column = $entity->propertyToColumn($property); $getter = 'get' . ucfirst($property); @@ -113,14 +112,13 @@ abstract class Mapper { $values .= '?'; // only append colon if there are more entries - if($i < count($properties)-1){ + if ($i < count($properties)-1) { $columns .= ','; $values .= ','; } $params[] = $entity->$getter(); $i++; - } $sql = 'INSERT INTO `' . $this->tableName . '`(' . @@ -148,13 +146,13 @@ abstract class Mapper { public function update(Entity $entity) { // if entity wasn't changed it makes no sense to run a db query $properties = $entity->getUpdatedFields(); - if(count($properties) === 0) { + if (count($properties) === 0) { return $entity; } // entity needs an id $id = $entity->getId(); - if($id === null){ + if ($id === null) { throw new \InvalidArgumentException( 'Entity which should be updated has no id'); } @@ -169,15 +167,14 @@ abstract class Mapper { // build the fields $i = 0; - foreach($properties as $property => $updated) { - + foreach ($properties as $property => $updated) { $column = $entity->propertyToColumn($property); $getter = 'get' . ucfirst($property); $columns .= '`' . $column . '` = ?'; // only append colon if there are more entries - if($i < count($properties)-1){ + if ($i < count($properties)-1) { $columns .= ','; } @@ -275,7 +272,7 @@ abstract class Mapper { $stmt = $this->execute($sql, $params, $limit, $offset); $row = $stmt->fetch(); - if($row === false || $row === null){ + if ($row === false || $row === null) { $stmt->closeCursor(); $msg = $this->buildDebugMessage( 'Did expect one result but found none when executing', $sql, $params, $limit, $offset @@ -285,7 +282,7 @@ abstract class Mapper { $row2 = $stmt->fetch(); $stmt->closeCursor(); //MDB2 returns null, PDO and doctrine false when no row is available - if(! ($row2 === false || $row2 === null)) { + if (! ($row2 === false || $row2 === null)) { $msg = $this->buildDebugMessage( 'Did not expect more than one result when executing', $sql, $params, $limit, $offset ); @@ -344,7 +341,7 @@ abstract class Mapper { $entities = []; - while($row = $stmt->fetch()){ + while ($row = $stmt->fetch()) { $entities[] = $this->mapRowToEntity($row); } @@ -370,6 +367,4 @@ abstract class Mapper { protected function findEntity($sql, array $params=[], $limit=null, $offset=null) { return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); } - - } diff --git a/lib/public/AppFramework/Db/MultipleObjectsReturnedException.php b/lib/public/AppFramework/Db/MultipleObjectsReturnedException.php index a3f211194c4..6b1a2ca7918 100644 --- a/lib/public/AppFramework/Db/MultipleObjectsReturnedException.php +++ b/lib/public/AppFramework/Db/MultipleObjectsReturnedException.php @@ -42,5 +42,4 @@ class MultipleObjectsReturnedException extends \Exception implements IMapperExce public function __construct($msg) { parent::__construct($msg); } - } diff --git a/lib/public/AppFramework/Db/QBMapper.php b/lib/public/AppFramework/Db/QBMapper.php index c7b4b877651..9b396965706 100644 --- a/lib/public/AppFramework/Db/QBMapper.php +++ b/lib/public/AppFramework/Db/QBMapper.php @@ -64,7 +64,7 @@ abstract class QBMapper { // if not given set the entity name to the class without the mapper part // cache it here for later use since reflection is slow - if($entityClass === null) { + if ($entityClass === null) { $this->entityClass = str_replace('Mapper', '', \get_class($this)); } else { $this->entityClass = $entityClass; @@ -117,7 +117,7 @@ abstract class QBMapper { $qb->insert($this->tableName); // build the fields - foreach($properties as $property => $updated) { + foreach ($properties as $property => $updated) { $column = $entity->propertyToColumn($property); $getter = 'get' . ucfirst($property); $value = $entity->$getter(); @@ -128,7 +128,7 @@ abstract class QBMapper { $qb->execute(); - if($entity->id === null) { + if ($entity->id === null) { // When autoincrement is used id is always an int $entity->setId((int)$qb->getLastInsertId()); } @@ -166,13 +166,13 @@ abstract class QBMapper { public function update(Entity $entity): Entity { // if entity wasn't changed it makes no sense to run a db query $properties = $entity->getUpdatedFields(); - if(\count($properties) === 0) { + if (\count($properties) === 0) { return $entity; } // entity needs an id $id = $entity->getId(); - if($id === null){ + if ($id === null) { throw new \InvalidArgumentException( 'Entity which should be updated has no id'); } @@ -186,7 +186,7 @@ abstract class QBMapper { $qb->update($this->tableName); // build the fields - foreach($properties as $property => $updated) { + foreach ($properties as $property => $updated) { $column = $entity->propertyToColumn($property); $getter = 'get' . ucfirst($property); $value = $entity->$getter(); @@ -217,11 +217,11 @@ abstract class QBMapper { protected function getParameterTypeForProperty(Entity $entity, string $property): int { $types = $entity->getFieldTypes(); - if(!isset($types[ $property ])) { + if (!isset($types[ $property ])) { return IQueryBuilder::PARAM_STR; } - switch($types[ $property ]) { + switch ($types[ $property ]) { case 'int': case 'integer': return IQueryBuilder::PARAM_INT; @@ -251,7 +251,7 @@ abstract class QBMapper { $cursor = $query->execute(); $row = $cursor->fetch(); - if($row === false) { + if ($row === false) { $cursor->closeCursor(); $msg = $this->buildDebugMessage( 'Did expect one result but found none when executing', $query @@ -261,7 +261,7 @@ abstract class QBMapper { $row2 = $cursor->fetch(); $cursor->closeCursor(); - if($row2 !== false) { + if ($row2 !== false) { $msg = $this->buildDebugMessage( 'Did not expect more than one result when executing', $query ); @@ -308,7 +308,7 @@ abstract class QBMapper { $entities = []; - while($row = $cursor->fetch()){ + while ($row = $cursor->fetch()) { $entities[] = $this->mapRowToEntity($row); } @@ -331,5 +331,4 @@ abstract class QBMapper { protected function findEntity(IQueryBuilder $query): Entity { return $this->mapRowToEntity($this->findOneQuery($query)); } - } |