Skip to content

Commit

Permalink
Merge pull request #14209 from jeabakker/features
Browse files Browse the repository at this point in the history
removed(core): InvalidParameterException as removed
  • Loading branch information
jeabakker committed Nov 9, 2022
2 parents 0b8d2a8 + 78c3ec4 commit 3b52d0f
Show file tree
Hide file tree
Showing 76 changed files with 362 additions and 389 deletions.
15 changes: 15 additions & 0 deletions docs/appendix/upgrade-notes/4.x-to-5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ Removed functions
* ``elgg.set_triggered_hook``
* ``elgg.trigger_hook`` use the ``trigger`` function provide by the ``elgg\hooks`` module

Exceptions
----------

The uses of exceptions in Elgg has been revisited. The ``\Elgg\Exceptions\InvalidParameterException`` has been removed and replaced with the correct exception.
Also the use of the ``\Elgg\Exceptions\InvalidArgumentException`` has been checked. In some cases the exception was replaced by a more appropriate exception.

.. note
All exceptions thrown in Elgg implement the ``\Elgg\Exceptions\ExceptionInterface`` if you wish to easily catch all Elgg exceptions.
Changes in functions
--------------------

Expand Down Expand Up @@ -373,6 +383,11 @@ Removed events
* ``access:collections:deletecollection, collection`` use the ``delete, access_collection`` sequence
* ``prepare, breadcrumbs`` use ``register, menu:breadcrumbs``

Removed exceptions
~~~~~~~~~~~~~~~~~~

* ``\Elgg\Exceptions\InvalidParameterException``

Constants
~~~~~~~~~

Expand Down
8 changes: 4 additions & 4 deletions engine/classes/Elgg/ActionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Elgg;

use Elgg\Exceptions\InvalidArgumentException;
use Elgg\Exceptions\DomainException;
use Elgg\Project\Paths;
use Elgg\Router\Middleware\ActionMiddleware;
use Elgg\Router\Middleware\AdminGatekeeper;
Expand Down Expand Up @@ -65,12 +65,12 @@ public function __construct(RouteRegistrationService $routes, HandlersService $h
* @param string $access Who is allowed to execute this action: public, logged_in, logged_out, admin. (default: logged_in)
*
* @return void
*
* @see elgg_register_action()
* @throws \Elgg\Exceptions\DomainException
* @see elgg_register_action()
*/
public function register(string $action, string $handler = '', string $access = 'logged_in'): void {
if (!in_array($access, self::$access_levels)) {
throw new InvalidArgumentException("Unrecognized value '{$access}' for \$access in " . __METHOD__);
throw new DomainException("Unrecognized value '{$access}' for \$access in " . __METHOD__);
}

// plugins are encouraged to call actions with a trailing / to prevent 301
Expand Down
6 changes: 3 additions & 3 deletions engine/classes/Elgg/Amd/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Elgg\Amd;

use Elgg\Exceptions\InvalidParameterException;
use Elgg\Exceptions\InvalidArgumentException;

/**
* Control configuration of RequireJS
Expand Down Expand Up @@ -93,14 +93,14 @@ public function removePath($name, $path = null) {
* - exports: string Name of the shimmed module to export
*
* @return void
* @throws InvalidParameterException
* @throws InvalidArgumentException
*/
public function addShim(string $name, array $config): void {
$deps = elgg_extract('deps', $config, []);
$exports = elgg_extract('exports', $config);

if (empty($deps) && empty($exports)) {
throw new InvalidParameterException("Shimmed modules must have deps or exports");
throw new InvalidArgumentException("Shimmed modules must have deps or exports");
}

$this->shim[$name] = [];
Expand Down
10 changes: 5 additions & 5 deletions engine/classes/Elgg/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,13 @@ public static function factory(array $spec = []) {
}

if ($spec['request']) {
if ($spec['request'] instanceof HttpRequest) {
$spec['request']->initializeTrustedProxyConfiguration($spec['internal_services']->config);
$spec['request']->correctBaseURL($spec['internal_services']->config);
$spec['internal_services']->set('request', $spec['request']);
} else {
if (!$spec['request'] instanceof HttpRequest) {
throw new InvalidArgumentException("Given request is not a " . HttpRequest::class);
}

$spec['request']->initializeTrustedProxyConfiguration($spec['internal_services']->config);
$spec['request']->correctBaseURL($spec['internal_services']->config);
$spec['internal_services']->set('request', $spec['request']);
}

$app = new self($spec['internal_services']);
Expand Down
7 changes: 4 additions & 3 deletions engine/classes/Elgg/Cache/LRUCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Elgg\Cache;

use Elgg\Exceptions\InvalidArgumentException;
use Elgg\Exceptions\RangeException;

/**
* Least Recently Used Cache
Expand Down Expand Up @@ -32,11 +32,12 @@ class LRUCache implements \ArrayAccess {
* Create a LRU Cache
*
* @param int $size The size of the cache
* @throws InvalidArgumentException
*
* @throws RangeException
*/
public function __construct(int $size) {
if ($size <= 0) {
throw new InvalidArgumentException();
throw new RangeException('"size" must be greater than 0');
}
$this->maximumSize = $size;
}
Expand Down
16 changes: 6 additions & 10 deletions engine/classes/Elgg/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

namespace Elgg\Collections;

use ArrayAccess;
use Countable;
use Elgg\Exceptions\InvalidParameterException;
use Elgg\Exceptions\InvalidArgumentException;
use Elgg\Exceptions\OutOfBoundsException;
use SeekableIterator;

/**
* A collection of unique items
*/
class Collection implements CollectionInterface,
ArrayAccess,
SeekableIterator,
Countable {
\ArrayAccess,
\SeekableIterator,
\Countable {

/**
* @var CollectionItemInterface[]
Expand Down Expand Up @@ -59,13 +55,13 @@ public function __construct($items = [], $item_class = null) {
* @param mixed $item Item
*
* @return void
* @throws \Elgg\Exceptions\InvalidParameterException
* @throws \Elgg\Exceptions\InvalidArgumentException
*/
protected function assertValidItem($item) {
$class = $this->item_class ? : CollectionItemInterface::class;
$class = $this->item_class ?? CollectionItemInterface::class;

if (!$item instanceof $class) {
throw new InvalidParameterException('Collection ' . __CLASS__ . ' only accepts instances of ' . $class);
throw new InvalidArgumentException('Collection ' . __CLASS__ . ' only accepts instances of ' . $class);
}
}

Expand Down
8 changes: 4 additions & 4 deletions engine/classes/Elgg/Database/Annotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Elgg\Database\Clauses\EntityWhereClause;
use Elgg\Database\Clauses\MetadataWhereClause;
use Elgg\Database\Clauses\RelationshipWhereClause;
use Elgg\Exceptions\DomainException;
use Elgg\Exceptions\InvalidArgumentException;
use Elgg\Exceptions\InvalidParameterException;
use Elgg\Exceptions\LogicException;

/**
Expand Down Expand Up @@ -47,12 +47,12 @@ public function count() {
* @param string $property_type 'attribute'|'metadata'|'annotation'
*
* @return int|float
* @throws InvalidParameterException
* @throws DomainException
*/
public function calculate($function, $property, $property_type = null) {

if (!in_array(strtolower($function), QueryBuilder::$calculations)) {
throw new InvalidArgumentException("'$function' is not a valid numeric function");
throw new DomainException("'{$function}' is not a valid numeric function");
}

if (!isset($property_type)) {
Expand All @@ -64,7 +64,7 @@ public function calculate($function, $property, $property_type = null) {
switch ($property_type) {
case 'attribute':
if (!in_array($property, \ElggEntity::PRIMARY_ATTR_NAMES)) {
throw new InvalidParameterException("'$property' is not a valid attribute");
throw new DomainException("'{$property}' is not a valid attribute");
}

$alias = $qb->joinEntitiesTable('n_table', 'entity_guid', 'inner', 'e');
Expand Down
11 changes: 5 additions & 6 deletions engine/classes/Elgg/Database/Clauses/AnnotationWhereClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Elgg\Database\Clauses;

use DateTime;
use Elgg\Database\QueryBuilder;
use Elgg\Exceptions\InvalidParameterException;
use Elgg\Exceptions\DomainException;

/**
* Builds queries for matching annotations against their properties
Expand Down Expand Up @@ -62,12 +61,12 @@ class AnnotationWhereClause extends WhereClause {
public $case_sensitive = true;

/**
* @var int|string|DateTime
* @var int|string|\DateTime
*/
public $created_after;

/**
* @var int|string|DateTime
* @var int|string|\DateTime
*/
public $created_before;

Expand Down Expand Up @@ -98,7 +97,7 @@ class AnnotationWhereClause extends WhereClause {

/**
* {@inheritdoc}
* @throws InvalidParameterException
* @throws DomainException
*/
public function prepare(QueryBuilder $qb, $table_alias = null) {
$alias = function ($column) use ($table_alias) {
Expand Down Expand Up @@ -126,7 +125,7 @@ public function prepare(QueryBuilder $qb, $table_alias = null) {

if ($this->sort_by_calculation) {
if (!in_array(strtolower($this->sort_by_calculation), QueryBuilder::$calculations)) {
throw new InvalidParameterException("'$this->sort_by_calculation' is not a valid numeric calculation formula");
throw new DomainException("'{$this->sort_by_calculation}' is not a valid numeric calculation formula");
}

$calculation = "{$this->sort_by_calculation}(CAST({$alias('value')} AS DECIMAL(10, 2)))";
Expand Down
6 changes: 3 additions & 3 deletions engine/classes/Elgg/Database/Clauses/ComparisonClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Elgg\Database\Clauses;

use Elgg\Database\QueryBuilder;
use Elgg\Exceptions\InvalidParameterException;
use Elgg\Exceptions\DomainException;
use Elgg\Values;

/**
Expand Down Expand Up @@ -56,7 +56,7 @@ public function __construct($x, $comparison, $y = null, $type = null, $case_sens
/**
* {@inheritdoc}
*
* @throws InvalidParameterException
* @throws DomainException
*/
public function prepare(QueryBuilder $qb, $table_alias = null) {
$x = $this->x;
Expand Down Expand Up @@ -157,7 +157,7 @@ public function prepare(QueryBuilder $qb, $table_alias = null) {
return "NOT EXISTS ($y)";

default :
throw new InvalidParameterException("'{$this->comparison}' is not a supported comparison operator");
throw new DomainException("'{$this->comparison}' is not a supported comparison operator");
}
}
}
11 changes: 6 additions & 5 deletions engine/classes/Elgg/Database/Clauses/EntitySortByClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace Elgg\Database\Clauses;

use Elgg\Database\QueryBuilder;
use Elgg\Exceptions\InvalidParameterException;
use ElggEntity;
use Elgg\Exceptions\DomainException;

/**
* Extends QueryBuilder with clauses necesary to sort entity lists by entity properties
Expand Down Expand Up @@ -43,11 +42,13 @@ class EntitySortByClause extends OrderByClause {

/**
* {@inheritdoc}
*
* @throws DomainException
*/
public function prepare(QueryBuilder $qb, $table_alias = null) {

if (!isset($this->property_type)) {
if (in_array($this->property, ElggEntity::PRIMARY_ATTR_NAMES)) {
if (in_array($this->property, \ElggEntity::PRIMARY_ATTR_NAMES)) {
$this->property_type = 'attribute';
} else {
$this->property_type = 'metadata';
Expand Down Expand Up @@ -77,8 +78,8 @@ public function prepare(QueryBuilder $qb, $table_alias = null) {
break;

case 'attribute':
if (!in_array($this->property, ElggEntity::PRIMARY_ATTR_NAMES)) {
throw new InvalidParameterException("'{$this->property}' is not a valid entity attribute");
if (!in_array($this->property, \ElggEntity::PRIMARY_ATTR_NAMES)) {
throw new DomainException("'{$this->property}' is not a valid entity attribute");
}

if ($qb->getTableName() !== QueryBuilder::TABLE_ENTITIES) {
Expand Down
8 changes: 4 additions & 4 deletions engine/classes/Elgg/Database/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Elgg\Database\Clauses\EntityWhereClause;
use Elgg\Database\Clauses\MetadataWhereClause;
use Elgg\Database\Clauses\RelationshipWhereClause;
use Elgg\Exceptions\DomainException;
use Elgg\Exceptions\InvalidArgumentException;
use Elgg\Exceptions\InvalidParameterException;
use Elgg\Exceptions\LogicException;

/**
Expand Down Expand Up @@ -51,12 +51,12 @@ public function count() {
* @param string $property_type 'attribute'|'metadata'|'annotation'
*
* @return string
* @throws InvalidParameterException
* @throws DomainException
*/
public function calculate($function, $property, $property_type = null) {

if (!in_array(strtolower($function), QueryBuilder::$calculations)) {
throw new InvalidArgumentException("'$function' is not a valid numeric function");
throw new DomainException("'{$function}' is not a valid numeric function");
}

if (!isset($property_type)) {
Expand All @@ -72,7 +72,7 @@ public function calculate($function, $property, $property_type = null) {
switch ($property_type) {
case 'attribute':
if (!in_array($property, \ElggEntity::PRIMARY_ATTR_NAMES)) {
throw new InvalidParameterException("'$property' is not a valid attribute");
throw new DomainException("'{$property}' is not a valid attribute");
}

$qb->addSelect("{$function}(e.{$property}) AS calculation");
Expand Down
10 changes: 5 additions & 5 deletions engine/classes/Elgg/Database/EntityTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Elgg\EventsService;
use Elgg\Exceptions\ClassException;
use Elgg\Exceptions\Database\UserFetchFailureException;
use Elgg\Exceptions\InvalidParameterException;
use Elgg\Exceptions\DomainException;
use Elgg\I18n\Translator;
use Elgg\Traits\Loggable;
use Elgg\Traits\TimeUsing;
Expand Down Expand Up @@ -119,11 +119,11 @@ public function __construct(
* @param string $class Entity class
*
* @return void
* @throws InvalidParameterException
* @throws DomainException
*/
public function setEntityClass(string $type, string $subtype, string $class = ''): void {
if (!in_array($type, Config::ENTITY_TYPES)) {
throw new InvalidParameterException("{$type} is not a valid entity type");
throw new DomainException("{$type} is not a valid entity type");
}

$this->entity_classes[$type][$subtype] = $class;
Expand Down Expand Up @@ -232,7 +232,7 @@ public function updateRow(int $guid, \stdClass $row) {
*
* @return \ElggEntity|null
* @throws ClassException
* @throws InvalidParameterException
* @throws DomainException
*/
public function rowToElggStar(\stdClass $row): ?\ElggEntity {
if (!isset($row->guid) || !isset($row->subtype)) {
Expand All @@ -256,7 +256,7 @@ public function rowToElggStar(\stdClass $row): ?\ElggEntity {
if (isset($map[$row->type])) {
$class_name = $map[$row->type];
} else {
throw new InvalidParameterException("Entity type {$row->type} is not supported.");
throw new DomainException("Entity type {$row->type} is not supported.");
}
}

Expand Down

0 comments on commit 3b52d0f

Please sign in to comment.