| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Daikon\Dbal\Migration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Assert\Assertion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Daikon\Dbal\Connector\ConnectorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Daikon\Dbal\Exception\MigrationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | trait MigrationTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $executedAt; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private $connector; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     public function __construct(\DateTimeImmutable $executedAt = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         $this->executedAt = $executedAt; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function execute(ConnectorInterface $connector, string $direction = self::MIGRATE_UP): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->connector = $connector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         if ($direction === self::MIGRATE_DOWN) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             Assertion::true($this->isReversible()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             Assertion::true($this->hasExecuted()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             $this->down(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $this->executedAt = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             Assertion::false($this->hasExecuted()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $this->up(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $this->executedAt = new \DateTimeImmutable('now'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 36 |  | View Code Duplication |     public function getName(): string | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $shortName = (new \ReflectionClass(static::class))->getShortName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if (!preg_match('#^(?<name>.+?)\d+$#', $shortName, $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             throw new MigrationException('Unexpected migration name in '.$shortName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return $matches['name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 45 |  | View Code Duplication |     public function getVersion(): int | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $shortName= (new \ReflectionClass(static::class))->getShortName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if (!preg_match('#(?<version>\d{14})$#', $shortName, $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             throw new MigrationException('Unexpected migration version in '.$shortName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return intval($matches['version']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function hasExecuted(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $this->executedAt instanceof \DateTimeImmutable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function toArray(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $arr = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             '@type' => static::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'name' => $this->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'version' => $this->getVersion(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'description' => $this->getDescription() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if ($this->hasExecuted()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $arr['executedAt']  = $this->executedAt->format('c'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  | 
            
                        
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.