Conditions | 13 |
Paths | 72 |
Total Lines | 69 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
38 | public function calculateRelationships(Individual $individual1, Individual $individual2, $recursion, $ancestor = false) { |
||
39 | |||
40 | $rows = Database::prepare( |
||
41 | "SELECT l_from, l_to FROM `##link` WHERE l_file = :tree_id AND l_type IN ('FAMS', 'FAMC')" |
||
42 | )->execute(array( |
||
43 | 'tree_id' => $individual1->getTree()->getTreeId(), |
||
44 | ))->fetchAll(); |
||
45 | |||
46 | // Optionally restrict the graph to the ancestors of the individuals. |
||
47 | if ($ancestor) { |
||
48 | $ancestors = $this->allAncestors($individual1->getXref(), $individual2->getXref(), $individual1->getTree()->getTreeId()); |
||
49 | $exclude = $this->excludeFamilies($individual1->getXref(), $individual2->getXref(), $individual1->getTree()->getTreeId()); |
||
50 | } else { |
||
51 | $ancestors = array(); |
||
52 | $exclude = array(); |
||
53 | } |
||
54 | |||
55 | $graph = array(); |
||
56 | |||
57 | foreach ($rows as $row) { |
||
58 | if (!$ancestors || in_array($row->l_from, $ancestors) && !in_array($row->l_to, $exclude)) { |
||
59 | $graph[$row->l_from][$row->l_to] = 1; |
||
60 | $graph[$row->l_to][$row->l_from] = 1; |
||
61 | } |
||
62 | } |
||
63 | |||
64 | $xref1 = $individual1->getXref(); |
||
65 | $xref2 = $individual2->getXref(); |
||
66 | $dijkstra = new Dijkstra($graph); |
||
67 | $paths = $dijkstra->shortestPaths($xref1, $xref2); |
||
68 | |||
69 | // Only process each exclusion list once; |
||
70 | $excluded = array(); |
||
71 | |||
72 | $queue = array(); |
||
73 | foreach ($paths as $path) { |
||
74 | // Insert the paths into the queue, with an exclusion list. |
||
75 | $queue[] = array('path' => $path, 'exclude' => array()); |
||
76 | // While there are un-extended paths |
||
77 | while (list(, $next) = each($queue)) { |
||
78 | // For each family on the path |
||
79 | for ($n = count($next['path']) - 2; $n >= 1; $n -= 2) { |
||
80 | $exclude = $next['exclude']; |
||
81 | if (count($exclude) >= $recursion) { |
||
82 | continue; |
||
83 | } |
||
84 | $exclude[] = $next['path'][$n]; |
||
85 | sort($exclude); |
||
86 | $tmp = implode('-', $exclude); |
||
87 | if (in_array($tmp, $excluded)) { |
||
88 | continue; |
||
89 | } else { |
||
90 | $excluded[] = $tmp; |
||
91 | } |
||
92 | // Add any new path to the queue |
||
93 | foreach ($dijkstra->shortestPaths($xref1, $xref2, $exclude) as $new_path) { |
||
94 | $queue[] = array('path' => $new_path, 'exclude' => $exclude); |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | // Extract the paths from the queue, removing duplicates. |
||
100 | $paths = array(); |
||
101 | foreach ($queue as $next) { |
||
102 | $paths[implode('-', $next['path'])] = $next['path']; |
||
103 | } |
||
104 | |||
105 | return $paths; |
||
106 | } |
||
107 | |||
215 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state