Skip to content

Commit

Permalink
Merge pull request #422 from Art4/417-deprecate-all-redmineapigetidby…
Browse files Browse the repository at this point in the history
…name-methods

Deprecate all `Redmine\Api\*::getIdBy...()` methods
  • Loading branch information
Art4 authored Jul 9, 2024
2 parents a4da285 + b11e318 commit e3de799
Show file tree
Hide file tree
Showing 21 changed files with 557 additions and 148 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated

- `Redmine\Api\CustomField::listing()` is deprecated, use `\Redmine\Api\CustomField::listNames()` instead.
- `Redmine\Api\CustomField::getIdByName()` is deprecated, use `\Redmine\Api\CustomField::listNames()` instead.
- `Redmine\Api\Group::listing()` is deprecated, use `\Redmine\Api\Group::listNames()` instead.
- `Redmine\Api\IssueCategory::listing()` is deprecated, use `\Redmine\Api\IssueCategory::listNamesByProject()` instead.
- `Redmine\Api\IssueCategory::getIdByName()` is deprecated, use `\Redmine\Api\IssueCategory::listNamesByProject()` instead.
- `Redmine\Api\IssueStatus::listing()` is deprecated, use `\Redmine\Api\IssueStatus::listNames()` instead.
- `Redmine\Api\IssueStatus::getIdByName()` is deprecated, use `\Redmine\Api\IssueStatus::listNames()` instead.
- `Redmine\Api\Project::listing()` is deprecated, use `\Redmine\Api\Project::listNames()` instead.
- `Redmine\Api\Project::getIdByName()` is deprecated, use `\Redmine\Api\Project::listNames()` instead.
- `Redmine\Api\Role::listing()` is deprecated, use `\Redmine\Api\Role::listNames()` instead.
- `Redmine\Api\TimeEntryActivity::listing()` is deprecated, use `\Redmine\Api\TimeEntryActivity::listNames()` instead.
- `Redmine\Api\TimeEntryActivity::getIdByName()` is deprecated, use `\Redmine\Api\TimeEntryActivity::listNames()` instead.
- `Redmine\Api\Tracker::listing()` is deprecated, use `\Redmine\Api\Tracker::listNames()` instead.
- `Redmine\Api\Tracker::getIdByName()` is deprecated, use `\Redmine\Api\Tracker::listNames()` instead.
- `Redmine\Api\User::listing()` is deprecated, use `\Redmine\Api\User::listLogins()` instead.
- `Redmine\Api\User::getIdByUsername()` is deprecated, use `\Redmine\Api\User::listLogins()` instead.
- `Redmine\Api\Version::listing()` is deprecated, use `\Redmine\Api\Version::listNamesByProject()` instead.
- `Redmine\Api\Version::getIdByName()` is deprecated, use `\Redmine\Api\Version::listNamesByProject()` instead.

## [v2.6.0](https://github.com/kbsali/php-redmine-api/compare/v2.5.0...v2.6.0) - 2024-03-25

Expand Down
35 changes: 24 additions & 11 deletions src/Redmine/Api/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,14 @@ public function listing($forceUpdate = false, array $params = [])
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNames()` instead.', E_USER_DEPRECATED);

if (empty($this->customFields) || $forceUpdate) {
$this->customFields = $this->list($params);
}
$ret = [];
foreach ($this->customFields['custom_fields'] as $e) {
$ret[$e['name']] = (int) $e['id'];
}

return $ret;
return $this->doListing($forceUpdate, $params);
}

/**
* Get a tracket id given its name.
* Get a custom field id given its name.
*
* @deprecated v2.7.0 Use listNames() instead.
* @see CustomField::listNames()
*
* @param string|int $name customer field name
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
Expand All @@ -132,11 +127,29 @@ public function listing($forceUpdate = false, array $params = [])
*/
public function getIdByName($name, array $params = [])
{
$arr = $this->listing(false, $params);
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNames()` instead.', E_USER_DEPRECATED);

$arr = $this->doListing(false, $params);

if (!isset($arr[$name])) {
return false;
}

return $arr[(string) $name];
}

private function doListing(bool $forceUpdate, array $params)
{
if (empty($this->customFields) || $forceUpdate) {
$this->customFields = $this->list($params);
}

$ret = [];

foreach ($this->customFields['custom_fields'] as $e) {
$ret[$e['name']] = (int) $e['id'];
}

return $ret;
}
}
39 changes: 32 additions & 7 deletions src/Redmine/Api/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public function setIssueStatus($id, $status)
$issueStatusApi = $this->getIssueStatusApi();

return $this->update($id, [
'status_id' => $issueStatusApi->getIdByName($status),
'status_id' => array_search($status, $issueStatusApi->listNames(), true),
]);
}

Expand Down Expand Up @@ -348,42 +348,67 @@ private function cleanParams(array $params = [])
if (isset($params['project'])) {
$projectApi = $this->getProjectApi();

$params['project_id'] = $projectApi->getIdByName($params['project']);
// TODO: project names are not unique; there could be collisions
$params['project_id'] = array_search(
$params['project'],
$projectApi->listNames(),
true,
);
unset($params['project']);
}

if (isset($params['category']) && isset($params['project_id'])) {
$issueCategoryApi = $this->getIssueCategoryApi();

$params['category_id'] = $issueCategoryApi->getIdByName($params['project_id'], $params['category']);
$params['category_id'] = array_search(
$params['category'],
$issueCategoryApi->listNamesByProject($params['project_id']),
true,
);
unset($params['category']);
}

if (isset($params['status'])) {
$issueStatusApi = $this->getIssueStatusApi();

$params['status_id'] = $issueStatusApi->getIdByName($params['status']);
$params['status_id'] = array_search(
$params['status'],
$issueStatusApi->listNames(),
true,
);
unset($params['status']);
}

if (isset($params['tracker'])) {
$trackerApi = $this->getTrackerApi();

$params['tracker_id'] = $trackerApi->getIdByName($params['tracker']);
$params['tracker_id'] = array_search(
$params['tracker'],
$trackerApi->listNames(),
true,
);
unset($params['tracker']);
}

if (isset($params['assigned_to'])) {
$userApi = $this->getUserApi();

$params['assigned_to_id'] = $userApi->getIdByUsername($params['assigned_to']);
$params['assigned_to_id'] = array_search(
$params['assigned_to'],
$userApi->listLogins(),
true,
);
unset($params['assigned_to']);
}

if (isset($params['author'])) {
$userApi = $this->getUserApi();

$params['author_id'] = $userApi->getIdByUsername($params['author']);
$params['author_id'] = array_search(
$params['author'],
$userApi->listLogins(),
true,
);
unset($params['author']);
}

Expand Down
33 changes: 23 additions & 10 deletions src/Redmine/Api/IssueCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,26 @@ public function listing($project, $forceUpdate = false)
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNamesByProject()` instead.', E_USER_DEPRECATED);

if (true === $forceUpdate || empty($this->issueCategories)) {
$this->issueCategories = $this->listByProject($project);
}
$ret = [];
foreach ($this->issueCategories['issue_categories'] as $e) {
$ret[$e['name']] = (int) $e['id'];
}

return $ret;
return $this->doListing($project, $forceUpdate);
}

/**
* Get a category id given its name and related project.
*
* @deprecated v2.7.0 Use listNamesByProject() instead.
* @see IssueCategory::listNamesByProject()
*
* @param string|int $project project id or literal identifier
* @param string $name
*
* @return int|false
*/
public function getIdByName($project, $name)
{
$arr = $this->listing($project);
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNamesByProject()` instead.', E_USER_DEPRECATED);

$arr = $this->doListing($project, false);

if (!isset($arr[$name])) {
return false;
}
Expand Down Expand Up @@ -283,4 +281,19 @@ public function remove($id, array $params = [])

return $this->lastResponse->getContent();
}

private function doListing($project, bool $forceUpdate)
{
if (true === $forceUpdate || empty($this->issueCategories)) {
$this->issueCategories = $this->listByProject($project);
}

$ret = [];

foreach ($this->issueCategories['issue_categories'] as $e) {
$ret[$e['name']] = (int) $e['id'];
}

return $ret;
}
}
33 changes: 23 additions & 10 deletions src/Redmine/Api/IssueStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,31 +110,44 @@ public function listing($forceUpdate = false)
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNames()` instead.', E_USER_DEPRECATED);

if (empty($this->issueStatuses) || $forceUpdate) {
$this->issueStatuses = $this->list();
}
$ret = [];
foreach ($this->issueStatuses['issue_statuses'] as $e) {
$ret[$e['name']] = (int) $e['id'];
}

return $ret;
return $this->doListing($forceUpdate);
}

/**
* Get a status id given its name.
*
* @deprecated v2.7.0 Use listNames() instead.
* @see IssueStatus::listNames()
*
* @param string $name
*
* @return int|false
*/
public function getIdByName($name)
{
$arr = $this->listing();
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNames()` instead.', E_USER_DEPRECATED);

$arr = $this->doListing(false);

if (!isset($arr[$name])) {
return false;
}

return $arr[(string) $name];
}

private function doListing(bool $forceUpdate)
{
if (empty($this->issueStatuses) || $forceUpdate) {
$this->issueStatuses = $this->list();
}

$ret = [];

foreach ($this->issueStatuses['issue_statuses'] as $e) {
$ret[$e['name']] = (int) $e['id'];
}

return $ret;
}
}
33 changes: 23 additions & 10 deletions src/Redmine/Api/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,26 @@ public function listing($forceUpdate = false, $reverse = true, array $params = [
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNames()` instead.', E_USER_DEPRECATED);

if (true === $forceUpdate || empty($this->projects)) {
$this->projects = $this->list($params);
}
$ret = [];
foreach ($this->projects['projects'] as $e) {
$ret[(int) $e['id']] = $e['name'];
}

return $reverse ? array_flip($ret) : $ret;
return $this->doListing($forceUpdate, $reverse, $params);
}

/**
* Get a project id given its name.
*
* @deprecated v2.7.0 Use listNames() instead.
* @see Project::listNames()
*
* @param string $name
* @param array $params to allow offset/limit (and more) to be passed
*
* @return int|bool
*/
public function getIdByName($name, array $params = [])
{
$arr = $this->listing(false, true, $params);
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNames()` instead.', E_USER_DEPRECATED);

$arr = $this->doListing(false, true, $params);

if (!isset($arr[$name])) {
return false;
}
Expand Down Expand Up @@ -447,4 +445,19 @@ public function remove($id)

return $this->lastResponse->getContent();
}

private function doListing(bool $forceUpdate, bool $reverse, array $params)
{
if (true === $forceUpdate || empty($this->projects)) {
$this->projects = $this->list($params);
}

$ret = [];

foreach ($this->projects['projects'] as $e) {
$ret[(int) $e['id']] = $e['name'];
}

return $reverse ? array_flip($ret) : $ret;
}
}
33 changes: 23 additions & 10 deletions src/Redmine/Api/TimeEntryActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,44 @@ public function listing($forceUpdate = false)
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNames()` instead.', E_USER_DEPRECATED);

if (empty($this->timeEntryActivities) || $forceUpdate) {
$this->timeEntryActivities = $this->list();
}
$ret = [];
foreach ($this->timeEntryActivities['time_entry_activities'] as $e) {
$ret[$e['name']] = (int) $e['id'];
}

return $ret;
return $this->doListing((bool) $forceUpdate);
}

/**
* Get a activities id given its name.
*
* @deprecated v2.7.0 Use listNames() instead.
* @see Project::listNames()
*
* @param string $name
*
* @return int|false
*/
public function getIdByName($name)
{
$arr = $this->listing();
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNames()` instead.', E_USER_DEPRECATED);

$arr = $this->doListing(false);

if (!isset($arr[$name])) {
return false;
}

return $arr[(string) $name];
}

private function doListing(bool $forceUpdate)
{
if (empty($this->timeEntryActivities) || $forceUpdate) {
$this->timeEntryActivities = $this->list();
}

$ret = [];

foreach ($this->timeEntryActivities['time_entry_activities'] as $e) {
$ret[$e['name']] = (int) $e['id'];
}

return $ret;
}
}
Loading

0 comments on commit e3de799

Please sign in to comment.