| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Kata\Algorithm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | final class Finder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** @var Person[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private $people; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 | 6 |  |     public function __construct(array $all_people) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 6 |  |         $this->people = $all_people; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 6 |  |     public function find(int $sorting_type): Comparator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         /** @var Comparator[] $sorted_people_from_older_to_younger */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 6 |  |         $sorted_people_from_older_to_younger = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 6 |  |         foreach ($this->people as $index => $person) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 5 |  |             for ($j = $index + 1; $j < count($this->people); $j++) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 4 |  |                 $person_to_compare_with = $this->people[$j]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 4 |  |                 $current_couple         = $this->sortFromOlderToYounger($person, $person_to_compare_with); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 4 |  |                 $sorted_people_from_older_to_younger[] = $current_couple; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 6 |  |         if (empty($sorted_people_from_older_to_younger)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |             return new Comparator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 4 |  |         $first_couple = $sorted_people_from_older_to_younger[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 4 |  |         $sorting_algorithm = $sorting_type == 1 ? SortClosest::class : SortFurthest::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 4 |  |         foreach ($sorted_people_from_older_to_younger as $current_couple) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 4 |  |             $first_couple = (new $sorting_algorithm())->__invoke($current_couple, $first_couple); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 4 |  |         return $first_couple; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param $person | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param $person_to_compare_with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return Comparator | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 4 |  |     private function sortFromOlderToYounger($person, $person_to_compare_with): Comparator | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 4 |  |         $result = new Comparator(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 4 |  |         if ($this->firstIsOlder($person, $person_to_compare_with)) | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 3 |  |             $result->first_person  = $person; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 3 |  |             $result->second_person = $person_to_compare_with; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         else | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 3 |  |             $result->first_person  = $person_to_compare_with; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 3 |  |             $result->second_person = $person; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 4 |  |         $this->addAgeDifference($result); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 4 |  |         return $result; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param $person | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param $person_to_compare_with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  |     private function firstIsOlder($person, $person_to_compare_with): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  |         return $person->birthDate < $person_to_compare_with->birthDate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |     private function addAgeDifference($result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  |         $result->age_difference = $result->second_person->birthDate->getTimestamp() - $result->first_person->birthDate->getTimestamp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 4 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  | 
            
                        
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: