| Total Complexity | 45 | 
| Total Lines | 224 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like RefWebTransformer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RefWebTransformer, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 29 | class RefWebTransformer implements TransformerInterface  | 
            ||
| 30 | { | 
            ||
| 31 | |||
| 32 | public $skipUnauthorised = true;  | 
            ||
| 33 | /**  | 
            ||
| 34 | * @var Logger  | 
            ||
| 35 | */  | 
            ||
| 36 | protected $log;  | 
            ||
| 37 | private $config;  | 
            ||
| 38 | /**  | 
            ||
| 39 | * @var string|string[]  | 
            ||
| 40 | */  | 
            ||
| 41 | private $domain;  | 
            ||
| 42 | /**  | 
            ||
| 43 | * @var string  | 
            ||
| 44 | */  | 
            ||
| 45 | private $url;  | 
            ||
| 46 | /**  | 
            ||
| 47 | * @var WebMapper  | 
            ||
| 48 | */  | 
            ||
| 49 | private $mapper;  | 
            ||
| 50 | /**  | 
            ||
| 51 | * @var array  | 
            ||
| 52 | */  | 
            ||
| 53 | private $data = [];  | 
            ||
| 54 | /**  | 
            ||
| 55 | * @var array  | 
            ||
| 56 | */  | 
            ||
| 57 | public $summaryLog = [];  | 
            ||
| 58 | |||
| 59 | /**  | 
            ||
| 60 | * RefWebTransformer constructor.  | 
            ||
| 61 | *  | 
            ||
| 62 | * @param LoggerInterface $log  | 
            ||
| 63 | */  | 
            ||
| 64 | public function __construct(LoggerInterface $log)  | 
            ||
| 65 |     { | 
            ||
| 66 | $this->log = $log;  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 67 | |||
| 68 | $this->config = Yaml::parseFile(__DIR__.'/resources/config_presse.yaml');  | 
            ||
| 69 | $this->data['newspaper'] = json_decode(file_get_contents(__DIR__.'/resources/data_newspapers.json'), true);  | 
            ||
| 70 | $this->data['scientific domain'] = json_decode(  | 
            ||
| 71 | file_get_contents(__DIR__.'/resources/data_scientific_domain.json'),  | 
            ||
| 72 | true  | 
            ||
| 73 | );  | 
            ||
| 74 | $this->data['scientific wiki'] = json_decode(  | 
            ||
| 75 | file_get_contents(__DIR__.'/resources/data_scientific_wiki.json'),  | 
            ||
| 76 | true  | 
            ||
| 77 | );  | 
            ||
| 78 | |||
| 79 | $this->mapper = new WebMapper(new Logger());  | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 | public function process(string $string)  | 
            ||
| 83 |     { | 
            ||
| 84 |         if (!$this->isTransformAutorized($string)) { | 
            ||
| 85 | return $string;  | 
            ||
| 86 | }  | 
            ||
| 87 | |||
| 88 | $publish = new PublisherAction($this->url);  | 
            ||
| 89 |         try { | 
            ||
| 90 | sleep(3);  | 
            ||
| 91 | $html = $publish->getHTMLSource();  | 
            ||
| 92 | $htmlData = $publish->extractWebData($html);  | 
            ||
| 93 |             $this->log->debug('htmlData', $htmlData); | 
            ||
| 94 |         } catch (Throwable $e) { | 
            ||
| 95 | // ne pas générer de lien brisé !!  | 
            ||
| 96 |             $this->log->notice('erreur sur extractWebData'); | 
            ||
| 97 | |||
| 98 | return $string;  | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | $mapData = $this->mapper->process($htmlData);  | 
            ||
| 102 | |||
| 103 | // check dataValide  | 
            ||
| 104 |         if (empty($mapData) || empty($mapData['url']) || empty($mapData['titre'])) { | 
            ||
| 105 |             $this->log->info('Mapping incomplet'); | 
            ||
| 106 | |||
| 107 | return $string;  | 
            ||
| 108 | }  | 
            ||
| 109 | |||
| 110 | $this->tagAndLog($mapData);  | 
            ||
| 111 | $this->addSummaryLog($mapData);  | 
            ||
| 112 | |||
| 113 | $template = $this->chooseTemplateByData($mapData);  | 
            ||
| 114 | |||
| 115 | $mapData = $this->replaceSitenameByConfig($mapData, $template);  | 
            ||
| 116 | |||
| 117 | $template->hydrate($mapData, true);  | 
            ||
| 118 | |||
| 119 | $serialized = $template->serialize(true);  | 
            ||
| 120 | $this->log->info($serialized."\n");  | 
            ||
| 121 | |||
| 122 | return $serialized;  | 
            ||
| 123 | }  | 
            ||
| 124 | |||
| 125 | protected function isTransformAutorized(string $string): bool  | 
            ||
| 126 |     { | 
            ||
| 127 |         if (!preg_match('#^http?s://[^ ]+$#i', $string)) { | 
            ||
| 128 | return false;  | 
            ||
| 129 | }  | 
            ||
| 130 | $this->url = $string;  | 
            ||
| 131 | $parseURL = parse_url($this->url);  | 
            ||
| 132 |         $this->domain = str_replace('www.', '', $parseURL['host']); | 
            ||
| 133 | |||
| 134 |         if (!isset($this->config[$this->domain])) { | 
            ||
| 135 |             $this->log->info("Domain ".$this->domain." non configuré\n"); | 
            ||
| 136 |             if ($this->skipUnauthorised) { | 
            ||
| 137 | return false;  | 
            ||
| 138 | }  | 
            ||
| 139 | echo "> Domaine ".Color::LIGHT_RED.$this->domain.Color::NORMAL." non configuré\n";  | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 | $this->config[$this->domain] = $this->config[$this->domain] ?? [];  | 
            ||
| 143 | $this->config[$this->domain] = is_array($this->config[$this->domain]) ? $this->config[$this->domain] : [];  | 
            ||
| 144 | |||
| 145 |         if ($this->config[$this->domain] === 'desactived' || isset($this->config[$this->domain]['desactived'])) { | 
            ||
| 146 |             $this->log->info("Domain ".$this->domain." desactivé\n"); | 
            ||
| 147 | |||
| 148 | return false;  | 
            ||
| 149 | }  | 
            ||
| 150 | |||
| 151 | return true;  | 
            ||
| 152 | }  | 
            ||
| 153 | |||
| 154 | private function tagAndLog(array $mapData)  | 
            ||
| 155 |     { | 
            ||
| 156 |         $this->log->debug('mapData', $mapData); | 
            ||
| 157 | |||
| 158 |         if (isset($mapData['DATA-ARTICLE']) && $mapData['DATA-ARTICLE']) { | 
            ||
| 159 |             $this->log->notice("Article OK"); | 
            ||
| 160 | }  | 
            ||
| 161 |         if (isset($this->data['newspaper'][$this->domain])) { | 
            ||
| 162 |             $this->log->notice('PRESSE'); | 
            ||
| 163 | }  | 
            ||
| 164 |         if ($this->isScientificDomain()) { | 
            ||
| 165 |             $this->log->notice('SCIENCE'); | 
            ||
| 166 | }  | 
            ||
| 167 | }  | 
            ||
| 168 | |||
| 169 | private function isScientificDomain(): bool  | 
            ||
| 170 |     { | 
            ||
| 171 |         if (isset($this->data['scientific domain'][$this->domain])) { | 
            ||
| 172 | return true;  | 
            ||
| 173 | }  | 
            ||
| 174 |         if (strpos('.revues.org', $this->domain) > 0) { | 
            ||
| 175 | return true;  | 
            ||
| 176 | }  | 
            ||
| 177 | |||
| 178 | return false;  | 
            ||
| 179 | }  | 
            ||
| 180 | |||
| 181 | private function addSummaryLog(array $mapData)  | 
            ||
| 182 |     { | 
            ||
| 183 | $this->summaryLog[] = $mapData['site'] ?? $mapData['périodique'] ?? '?';  | 
            ||
| 184 | }  | 
            ||
| 185 | |||
| 186 | /**  | 
            ||
| 187 | * todo refac lisible  | 
            ||
| 188 | */  | 
            ||
| 189 | private function chooseTemplateByData(array $mapData): AbstractWikiTemplate  | 
            ||
| 209 | }  | 
            ||
| 210 | |||
| 211 | /**  | 
            ||
| 212 | * Logique : remplacement titre périodique ou nom du site  | 
            ||
| 213 | *  | 
            ||
| 214 | * @param array $mapData  | 
            ||
| 215 | * @param $template  | 
            ||
| 216 | *  | 
            ||
| 217 | * @return array  | 
            ||
| 218 | */  | 
            ||
| 219 | private function replaceSitenameByConfig(array $mapData, $template): array  | 
            ||
| 256 | 
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.