Skip to content

Commit d6c1ecc

Browse files
committed
Make code look nice.
1 parent cbc8f26 commit d6c1ecc

File tree

8 files changed

+86
-84
lines changed

8 files changed

+86
-84
lines changed

src/Climber.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Livy\Climber;
44

5-
use \Zenodorus as Z;
5+
use Zenodorus as Z;
66

77
/**
88
* Class Climber
@@ -141,6 +141,16 @@ function ($data) {
141141
);
142142
}
143143

144+
public function setCurrentUrl($url, $strict = true)
145+
{
146+
if (count($currentLeaves = $this->getLeafByTarget($url, $strict)) > 0) {
147+
foreach ($currentLeaves as $leaf) {
148+
$this->activate($leaf);
149+
}
150+
}
151+
return $this;
152+
}
153+
144154
public function getLeafByTarget(string $target, bool $strict = true)
145155
{
146156
$leaves = [];
@@ -173,21 +183,6 @@ public function getLeafByTarget(string $target, bool $strict = true)
173183
return $leaves;
174184
}
175185

176-
public function setCurrentUrl($url, $strict = true)
177-
{
178-
if (count($currentLeaves = $this->getLeafByTarget($url, $strict)) > 0) {
179-
foreach ($currentLeaves as $leaf) {
180-
$this->activate($leaf);
181-
}
182-
}
183-
return $this;
184-
}
185-
186-
public function isActivated()
187-
{
188-
return count(array_column($this->tree->grow(), 3)) > 0;
189-
}
190-
191186
public function activate(int $hint)
192187
{
193188
if ($leaf = $this->tree->getLeaf($hint)) {
@@ -227,6 +222,11 @@ public function hook(string $location, $callback, $order = false)
227222
return null;
228223
}
229224

225+
public function isActivated()
226+
{
227+
return count(array_column($this->tree->grow(), 3)) > 0;
228+
}
229+
230230
public function __toString()
231231
{
232232
return $this->element();

src/Spotter/Forester.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ protected function convertToFormat($data)
4040
}
4141

4242
return [
43-
'parent' => $data[0],
44-
'data' => isset($data[2]) ? $data[2] : [],
43+
'parent' => $data[0],
44+
'data' => isset($data[2]) ? $data[2] : [],
4545
];
4646
}
4747
}

src/Spotter/Spotter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public function __construct($seed)
1414
$this->seed = $seed;
1515
}
1616

17-
abstract protected function soil();
18-
1917
public function germinate()
2018
{
2119
// Only pass this on if it's a valid seed.
2220
$this->sprout = $this->seed ? $this->soil() : null;
2321
return $this->sprout;
2422
}
23+
24+
abstract protected function soil();
2525
}

src/Spotter/WordPress.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
class WordPress extends Spotter
66
{
77
public $expected = [
8-
'ID' => 'id',
8+
'ID' => 'id',
99
'menu_item_parent' => 'parent',
10-
'menu_order' => 'order',
11-
'url' => 'target',
12-
'title' => 'name',
10+
'menu_order' => 'order',
11+
'url' => 'target',
12+
'title' => 'name',
1313
];
1414

1515
/**
@@ -20,7 +20,7 @@ protected function soil()
2020
$temp = [];
2121
foreach ($this->seed as $item) {
2222
$temp[$item->ID] = [];
23-
23+
2424
// If there's no parent, `parent` should be null
2525
if ($item->menu_item_parent == 0) {
2626
$item->menu_item_parent = null;

src/Tree.php

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Livy\Climber\Spotter\Forester;
66
use Livy\Climber\Spotter\Spotter;
7-
use \Zenodorus as Z;
7+
use Zenodorus as Z;
88

99
/**
1010
* This class generates data trees for processing by Climber.
@@ -172,34 +172,19 @@ protected function plant()
172172
return null;
173173
}
174174

175-
public function getLeafPath(int $id, array $ancestors = [])
175+
public function getLeafSiblings(int $id, bool $exclude = null)
176176
{
177-
if (null === $this->getLeaf($id)
178-
|| null === $this->getLeafContent($id, 0)) {
179-
// This item doesn't exist or has no ancestors.
180-
// Either we've reached the last step in our path, we were passed
181-
// a bad leaf, or this leaf has no parent.
182-
return array_reverse($ancestors);
183-
}
184-
185-
array_push($ancestors, $this->getLeafContent($id, 0));
186-
187-
return $this->getLeafPath($this->getLeafContent($id, 0), $ancestors);
188-
}
177+
$parent = $this->getLeafContent($id, 'parent');
178+
$tree = $this->tree;
179+
$siblings = array_filter($tree, function ($item) use ($parent) {
180+
return $item[0] == $parent;
181+
});
189182

190-
public function isLeafChildOf(int $leaf, int $parent)
191-
{
192-
$ancestors = $this->getLeafPath($leaf);
193-
if (count($ancestors) > 0 && in_array($parent, $ancestors)) {
194-
return true;
183+
if ($exclude) {
184+
unset($siblings[$id]);
195185
}
196186

197-
return false;
198-
}
199-
200-
public function getLeaf(int $id)
201-
{
202-
return isset($this->tree[$id]) ? $this->tree[$id] : null;
187+
return $siblings;
203188
}
204189

205190
public function getLeafContent(int $id, $slot, $data = null)
@@ -242,19 +227,9 @@ protected function query($slot)
242227
return false;
243228
}
244229

245-
public function getLeafSiblings(int $id, bool $exclude = null)
230+
public function getLeaf(int $id)
246231
{
247-
$parent = $this->getLeafContent($id, 'parent');
248-
$tree = $this->tree;
249-
$siblings = array_filter($tree, function ($item) use ($parent) {
250-
return $item[0] == $parent;
251-
});
252-
253-
if ($exclude) {
254-
unset($siblings[$id]);
255-
}
256-
257-
return $siblings;
232+
return isset($this->tree[$id]) ? $this->tree[$id] : null;
258233
}
259234

260235
public function setLeaf(int $id, array...$actions)
@@ -624,16 +599,6 @@ protected function setActive(int $id, string $value)
624599
);
625600
}
626601

627-
/**
628-
* We want to know if we're the original or not, so set `$clone`.
629-
*
630-
* @return void
631-
*/
632-
protected function __clone()
633-
{
634-
$this->clone = true;
635-
}
636-
637602
/**
638603
* Takes a leaf and returns a new Tree composed of only the children of that leaf.
639604
*
@@ -664,6 +629,31 @@ public function subtree(int $root, bool $includeRoot = false)
664629
);
665630
}
666631

632+
public function isLeafChildOf(int $leaf, int $parent)
633+
{
634+
$ancestors = $this->getLeafPath($leaf);
635+
if (count($ancestors) > 0 && in_array($parent, $ancestors)) {
636+
return true;
637+
}
638+
639+
return false;
640+
}
641+
642+
public function getLeafPath(int $id, array $ancestors = [])
643+
{
644+
if (null === $this->getLeaf($id)
645+
|| null === $this->getLeafContent($id, 0)) {
646+
// This item doesn't exist or has no ancestors.
647+
// Either we've reached the last step in our path, we were passed
648+
// a bad leaf, or this leaf has no parent.
649+
return array_reverse($ancestors);
650+
}
651+
652+
array_push($ancestors, $this->getLeafContent($id, 0));
653+
654+
return $this->getLeafPath($this->getLeafContent($id, 0), $ancestors);
655+
}
656+
667657
/**
668658
* Sometimes we don't want to be a clone after all.
669659
*/
@@ -672,6 +662,16 @@ public function declone()
672662
$this->clone = false;
673663
}
674664

665+
/**
666+
* We want to know if we're the original or not, so set `$clone`.
667+
*
668+
* @return void
669+
*/
670+
protected function __clone()
671+
{
672+
$this->clone = true;
673+
}
674+
675675
/**
676676
* Remove a leaf from the tree.
677677
*

src/func/common.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
use \Livy\Climber;
2+
3+
use Livy\Climber;
34

45
// phpcs:disable
56
$GLOBALS['livy_climber_helper_func_loaded']['common'] = true;
@@ -8,10 +9,10 @@
89
/**
910
* Returns a Climber object.
1011
*
11-
* @param Spotter/[ClassName] $spotter An instance of a class that
12+
* @param Livy\Climber\Spotter\[ClassName] $spotter An instance of a class that
1213
* extends Spotter/Spotter.
1314
* @param string $currentUrl The URL of the current page.
14-
* @return Climber|boolean
15+
* @return Livy\Climber\Climber|boolean
1516
*/
1617
function pulley__get_menu($spotter, string $currentUrl = null)
1718
{
@@ -32,10 +33,10 @@ function pulley__get_menu($spotter, string $currentUrl = null)
3233
*
3334
* @see pulley__getMenu()
3435
*
35-
* @param Spotter/[ClassName] $spotter An instance of a class that
36+
* @param Livy\Climber\Spotter\[ClassName] $spotter An instance of a class that
3637
* extends Spotter/Spotter.
3738
* @param string $currentUrl The URL of the current page.
38-
* @return string
39+
* @return void
3940
*/
4041
function pulley__menu($spotter, string $currentUrl = null)
4142
{

src/func/wp.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
use \Livy\Climber;
2+
3+
use Livy\Climber;
34

45
// phpcs:disable
56
$GLOBALS['livy_climber_helper_func_loaded']['wp'] = true;
@@ -20,8 +21,8 @@
2021
* `wp_get_nave_menu_items()`.
2122
*
2223
* @param int|string|WP_Term $menu
23-
* @param string $currentUrl
24-
* @return Climber
24+
* @param string $currentUrl
25+
* @return Livy\Climber\Climber
2526
*/
2627
function pulley__wp_get_menu($menu, string $currentUrl = null)
2728
{
@@ -37,7 +38,7 @@ function pulley__wp_get_menu($menu, string $currentUrl = null)
3738
* @see pulley__wp_get_menu()
3839
*
3940
* @param int|string|WP_Term $menu
40-
* @param string $currentUrl
41+
* @param string $currentUrl
4142
* @return void
4243
*/
4344
function pulley__wp_menu($menu, string $currentUrl = null)
@@ -53,7 +54,7 @@ function pulley__wp_menu($menu, string $currentUrl = null)
5354
*
5455
* @param string $location
5556
* @param string $currentUrl
56-
* @return Climber
57+
* @return Livy\Climber\Climber|boolean
5758
*/
5859
function pulley__wp_get_menu_by_location(
5960
string $location,

tests/shims.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ class WP_Term
114114
* validated as `WP_Term` object.
115115
*
116116
* @param integer $integer
117-
* @param string $type
118-
* @return void
117+
* @param string $type
118+
* @return bool|WP_Term
119119
*/
120120
function get_term($integer, $type)
121121
{

0 commit comments

Comments
 (0)