| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace DanielWerner\PhpQualityTools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class PhpQualityTools | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @var string $srcDirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $srcDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * PhpQualityTools constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @param string $srcDirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function __construct(string $srcDirectory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         echo sprintf('Using source directory: "%s"', $srcDirectory) . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->srcDirectory = $srcDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param string $destination | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     public function install($destination) | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $this->copyStubs($destination); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $this->setUpComposerJson($destination); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         echo 'Done.' . PHP_EOL; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $destination | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected function copyStubs(string $destination) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         echo 'Copying configuration files.' . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->createDestinationIfNotExist($destination); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->copyFile('phpmd.xml', $destination); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->copyFile('phpcs.xml', $destination); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param string $destination | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     protected function setUpComposerJson(string $destination) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         echo 'Setting up composer.json scripts.' . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $composerJson = $destination . '/composer.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if (!file_exists($composerJson)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             throw new \Exception('File composer.json is missed! Please ensure that you are in root folder.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $composerSettings = $this->readComposerJson($composerJson); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (is_null($composerSettings)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             throw new \Exception('File composer.json is corrupted!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if (empty($composerSettings->scripts)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $composerSettings->scripts = new stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $composerSettings->scripts = (object) array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             (array) $composerSettings->scripts, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             (array) $this->getComposerScripts() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (!$this->writeComposerJson($composerJson, $composerSettings)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             throw new \Exception('Cannot write new composer.json!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     protected function getComposerScripts(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             "inspect" => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 sprintf("vendor/bin/phpcs %s", $this->srcDirectory), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 sprintf("vendor/bin/phpstan analyze %s", $this->srcDirectory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             "inspect-fix" => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 sprintf("vendor/bin/php-cs-fixer fix %s", $this->srcDirectory), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 sprintf("vendor/bin/phpcbf %s", $this->srcDirectory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             "insights" => sprintf("vendor/bin/phpmd %s text phpmd.xml", $this->srcDirectory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param string $composerJson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     protected function readComposerJson(string $composerJson): \stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $content = file_get_contents($composerJson); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         if (!$content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             throw new \Exception('File composer.json is empty!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         return json_decode($content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param string $composerJson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param array $composerSettings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return bool|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function writeComposerJson(string $composerJson, \stdClass $composerSettings) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return file_put_contents( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $composerJson, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             json_encode( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 $composerSettings, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 JSON_PRETTY_PRINT | | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 JSON_UNESCAPED_LINE_TERMINATORS | | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 JSON_UNESCAPED_SLASHES | | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 JSON_UNESCAPED_UNICODE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     private function createDestinationIfNotExist($destination)  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         if (is_null($destination)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             throw new \Exception('Failed to create required folders!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         if (!file_exists($destination)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             if (!mkdir($destination, 0777, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 throw new \Exception('Failed to create required folders! Please check your write permission.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         }     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     private function copyFile($filename, $destination)  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         if (!file_exists(__DIR__ . "/../$filename") || !copy(__DIR__ . "/../$filename", $destination . "/$filename")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             throw new \Exception(sprintf("File %s cannot be created! Please check your write permission.", $filename)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  |