1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-10 14:51:06 +01:00

Use head_key() and last_key() to explicitly communicate intent

Summary:
PHP arrays have an internal "current position" marker. (I think because foreach() wasn't introduced until PHP 4 and there was no way to get rid of it by then?)

A few functions affect the position of the marker, like reset(), end(), each(), next(), and prev(). A few functions read the position of the marker, like each(), next(), prev(), current() and key().

For the most part, no one uses any of this because foreach() is vastly easier and more natural. However, we sometimes want to select the first or last key from an array. Since key() returns the key //at the current position//, and you can't guarantee that no one will introduce some next() calls somewhere, the right way to do this is reset() + key(). This is cumbesome, so we introduced head_key() and last_key() (like head() and last()) in D2161.

Switch all the reset()/end() + key() (or omitted reset() since I was feeling like taking risks + key()) calls to head_key() or last_key().

Test Plan: Verified most of these by visiting the affected pages.

Reviewers: btrahan, vrana, jungejason, Koolvin

Reviewed By: jungejason

CC: aran

Differential Revision: https://secure.phabricator.com/D2169
This commit is contained in:
epriestley 2012-04-09 11:08:59 -07:00
parent db2fef4c87
commit a5903d2a53
9 changed files with 13 additions and 18 deletions

View file

@ -1,7 +1,7 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -92,8 +92,7 @@ final class DarkConsoleCore {
$visible = $user->getConsoleVisible();
if (!isset($plugins[$selected])) {
reset($plugins);
$selected = key($plugins);
$selected = head_key($plugins);
}
$tabs = array();

View file

@ -34,7 +34,7 @@ final class PhabricatorConduitConsoleController
$methods = $this->getAllMethods();
if (empty($methods[$this->method])) {
$this->method = key($methods);
$this->method = head_key($methods);
}
$this->setFilter('method/'.$this->method);

View file

@ -52,8 +52,7 @@ final class DiffusionExternalController extends DiffusionController {
}
arsort($matches);
reset($matches);
$best_match = key($matches);
$best_match = head_key($matches);
if ($best_match) {
$repository = $repositories[$best_match];

View file

@ -46,7 +46,7 @@ final class HeraldHomeController extends HeraldController {
$content_type_map = HeraldContentTypeConfig::getContentTypeMap();
if (empty($content_type_map[$this->contentType])) {
$this->contentType = key($content_type_map);
$this->contentType = head_key($content_type_map);
}
$content_desc = $content_type_map[$this->contentType];

View file

@ -33,8 +33,7 @@ final class HeraldNewController extends HeraldController {
$content_type_map = HeraldContentTypeConfig::getContentTypeMap();
if (empty($content_type_map[$this->contentType])) {
reset($content_type_map);
$this->contentType = key($content_type_map);
$this->contentType = head_key($content_type_map);
}
$rule_type_map = HeraldRuleTypeConfig::getRuleTypeMap();

View file

@ -173,7 +173,7 @@ final class ManiphestReportController extends ManiphestController {
$week = null;
$month = null;
$last = key($stats) - 1;
$last = last_key($stats) - 1;
$period = $template;
foreach ($stats as $bucket => $info) {
@ -182,7 +182,7 @@ final class ManiphestReportController extends ManiphestController {
$week_bucket = phabricator_format_local_time(
$epoch,
$user,
'W');
'YW');
if ($week_bucket != $last_week) {
if ($week) {
$rows[] = $this->formatBurnRow(
@ -198,7 +198,7 @@ final class ManiphestReportController extends ManiphestController {
$month_bucket = phabricator_format_local_time(
$epoch,
$user,
'm');
'Ym');
if ($month_bucket != $last_month) {
if ($month) {
$rows[] = $this->formatBurnRow(

View file

@ -46,8 +46,7 @@ final class PhabricatorRepositoryEditController
$this->repository = $repository;
if (!isset($views[$this->view])) {
reset($views);
$this->view = key($views);
$this->view = head_key($views);
}
$nav = new AphrontSideNavView();

View file

@ -33,8 +33,8 @@ final class PhabricatorRepositorySvnCommitDiscoveryDaemon
$uri);
$results = $this->parseSVNLogXML($xml);
$commit = key($results);
$epoch = reset($results);
$commit = head_key($results);
$epoch = head($results);
if ($this->isKnownCommit($commit)) {
return false;

View file

@ -41,8 +41,7 @@ final class PhabricatorUIExampleRenderController
}
if (!$selected) {
reset($classes);
$selected = key($classes);
$selected = head_key($classes);
}
$nav = new AphrontSideNavView();