Skip to content

Commit a5903d2

Browse files
author
epriestley
committed
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
1 parent db2fef4 commit a5903d2

File tree

9 files changed

+13
-18
lines changed

9 files changed

+13
-18
lines changed

src/aphront/console/core/DarkConsoleCore.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* Copyright 2011 Facebook, Inc.
4+
* Copyright 2012 Facebook, Inc.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -92,8 +92,7 @@ public function render(AphrontRequest $request) {
9292
$visible = $user->getConsoleVisible();
9393

9494
if (!isset($plugins[$selected])) {
95-
reset($plugins);
96-
$selected = key($plugins);
95+
$selected = head_key($plugins);
9796
}
9897

9998
$tabs = array();

src/applications/conduit/controller/console/PhabricatorConduitConsoleController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function processRequest() {
3434

3535
$methods = $this->getAllMethods();
3636
if (empty($methods[$this->method])) {
37-
$this->method = key($methods);
37+
$this->method = head_key($methods);
3838
}
3939
$this->setFilter('method/'.$this->method);
4040

src/applications/diffusion/controller/external/DiffusionExternalController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public function processRequest() {
5252
}
5353

5454
arsort($matches);
55-
reset($matches);
56-
$best_match = key($matches);
55+
$best_match = head_key($matches);
5756

5857
if ($best_match) {
5958
$repository = $repositories[$best_match];

src/applications/herald/controller/home/HeraldHomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function processRequest() {
4646

4747
$content_type_map = HeraldContentTypeConfig::getContentTypeMap();
4848
if (empty($content_type_map[$this->contentType])) {
49-
$this->contentType = key($content_type_map);
49+
$this->contentType = head_key($content_type_map);
5050
}
5151
$content_desc = $content_type_map[$this->contentType];
5252

src/applications/herald/controller/new/HeraldNewController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public function processRequest() {
3333

3434
$content_type_map = HeraldContentTypeConfig::getContentTypeMap();
3535
if (empty($content_type_map[$this->contentType])) {
36-
reset($content_type_map);
37-
$this->contentType = key($content_type_map);
36+
$this->contentType = head_key($content_type_map);
3837
}
3938

4039
$rule_type_map = HeraldRuleTypeConfig::getRuleTypeMap();

src/applications/maniphest/controller/report/ManiphestReportController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function renderBurn() {
173173
$week = null;
174174
$month = null;
175175

176-
$last = key($stats) - 1;
176+
$last = last_key($stats) - 1;
177177
$period = $template;
178178

179179
foreach ($stats as $bucket => $info) {
@@ -182,7 +182,7 @@ public function renderBurn() {
182182
$week_bucket = phabricator_format_local_time(
183183
$epoch,
184184
$user,
185-
'W');
185+
'YW');
186186
if ($week_bucket != $last_week) {
187187
if ($week) {
188188
$rows[] = $this->formatBurnRow(
@@ -198,7 +198,7 @@ public function renderBurn() {
198198
$month_bucket = phabricator_format_local_time(
199199
$epoch,
200200
$user,
201-
'm');
201+
'Ym');
202202
if ($month_bucket != $last_month) {
203203
if ($month) {
204204
$rows[] = $this->formatBurnRow(

src/applications/repository/controller/edit/PhabricatorRepositoryEditController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public function processRequest() {
4646
$this->repository = $repository;
4747

4848
if (!isset($views[$this->view])) {
49-
reset($views);
50-
$this->view = key($views);
49+
$this->view = head_key($views);
5150
}
5251

5352
$nav = new AphrontSideNavView();

src/applications/repository/daemon/commitdiscovery/svn/PhabricatorRepositorySvnCommitDiscoveryDaemon.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ protected function discoverCommits() {
3333
$uri);
3434

3535
$results = $this->parseSVNLogXML($xml);
36-
$commit = key($results);
37-
$epoch = reset($results);
36+
$commit = head_key($results);
37+
$epoch = head($results);
3838

3939
if ($this->isKnownCommit($commit)) {
4040
return false;

src/applications/uiexample/controller/render/PhabricatorUIExampleRenderController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public function processRequest() {
4141
}
4242

4343
if (!$selected) {
44-
reset($classes);
45-
$selected = key($classes);
44+
$selected = head_key($classes);
4645
}
4746

4847
$nav = new AphrontSideNavView();

0 commit comments

Comments
 (0)