Conditions | 29 |
Paths | 1256 |
Total Lines | 286 |
Lines | 110 |
Ratio | 38.46 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
287 | public function save($data) |
||
288 | { |
||
289 | // When editing a package, find the original path |
||
290 | $app = JFactory::getApplication('administrator'); |
||
291 | $originalId = $app->getUserState('com_localise.edit.packagefile.id'); |
||
292 | $oldpath = null; |
||
293 | |||
294 | $originalId = is_array($originalId) && count($originalId) > 0 ? |
||
295 | $originalId[0] : $originalId; |
||
296 | |||
297 | View Code Duplication | if (!empty($originalId)) |
|
298 | { |
||
299 | $db = JFactory::getDbo(); |
||
300 | $query = $db->getQuery(true) |
||
301 | ->select($db->quoteName('path')) |
||
302 | ->from($db->quoteName('#__localise')) |
||
303 | ->where($db->quoteName('id') . ' = ' . $originalId); |
||
304 | $db->setQuery($query); |
||
305 | |||
306 | $oldpath = $db->loadResult('path'); |
||
307 | } |
||
308 | |||
309 | // Get the package name |
||
310 | $name = $data['name']; |
||
311 | |||
312 | // Get the package |
||
313 | $package = $this->getItem(); |
||
314 | $path = JPATH_COMPONENT_ADMINISTRATOR . "/packages/$name.xml"; |
||
315 | $manifest = $name; |
||
316 | |||
317 | // $client = $package->client ? $package->client : 'site'; |
||
318 | |||
319 | if ($package->standalone) |
||
320 | { |
||
321 | $title = $name; |
||
322 | $description = $data['description']; |
||
323 | |||
324 | $dom = new DOMDocument('1.0', 'utf-8'); |
||
325 | |||
326 | // Create simple XML element and base package tag |
||
327 | $packageXml = $dom->createElement('package'); |
||
328 | |||
329 | // Add main package information |
||
330 | $titleElement = $dom->createElement('title', $title); |
||
331 | $descriptionElement = $dom->createElement('description', $description); |
||
332 | $manifestElement = $dom->createElement('manifest', $manifest); |
||
333 | $versionElement = $dom->createElement('version', $data['version']); |
||
334 | $packversionElement = $dom->createElement('packversion', $data['packversion']); |
||
335 | $authorElement = $dom->createElement('author', $data['author']); |
||
336 | $licenseElement = $dom->createElement('license', $data['license']); |
||
337 | $authorEmailElement = $dom->createElement('authoremail', $data['authoremail']); |
||
338 | $authorUrlElement = $dom->createElement('authorurl', $data['authorurl']); |
||
339 | $languageElement = $dom->createElement('language', $data['language']); |
||
340 | $copyrightElement = $dom->createElement('copyright', $data['copyright']); |
||
341 | $packagerElement = $dom->createElement('packager', $data['packager']); |
||
342 | $packagerUrlElement = $dom->createElement('packagerurl', $data['packagerurl']); |
||
343 | $servernameElement = $dom->createElement('servername', $data['servername']); |
||
344 | $serverurlElement = $dom->createElement('serverurl', $data['serverurl']); |
||
345 | |||
346 | // Set the client attribute on the manifest element |
||
347 | |||
348 | // $clientAttribute = $dom->createAttribute('client'); |
||
349 | |||
350 | // $clientAttribute->value = $client; |
||
351 | |||
352 | // $manifestElement->appendChild($clientAttribute); |
||
353 | |||
354 | // Add all the elements to the parent <package> tag |
||
355 | $packageXml->appendChild($titleElement); |
||
356 | $packageXml->appendChild($descriptionElement); |
||
357 | $packageXml->appendChild($manifestElement); |
||
358 | $packageXml->appendChild($versionElement); |
||
359 | $packageXml->appendChild($packversionElement); |
||
360 | $packageXml->appendChild($authorElement); |
||
361 | $packageXml->appendChild($copyrightElement); |
||
362 | $packageXml->appendChild($licenseElement); |
||
363 | $packageXml->appendChild($authorEmailElement); |
||
364 | $packageXml->appendChild($authorUrlElement); |
||
365 | $packageXml->appendChild($languageElement); |
||
366 | $packageXml->appendChild($copyrightElement); |
||
367 | $packageXml->appendChild($packagerElement); |
||
368 | $packageXml->appendChild($packagerUrlElement); |
||
369 | $packageXml->appendChild($servernameElement); |
||
370 | $packageXml->appendChild($serverurlElement); |
||
371 | |||
372 | $administrator = array(); |
||
373 | $site = array(); |
||
374 | |||
375 | View Code Duplication | foreach ($data['translations'] as $translation) |
|
376 | { |
||
377 | if (preg_match('/^site_(.*)$/', $translation, $matches)) |
||
378 | { |
||
379 | $site[] = $matches[1]; |
||
380 | } |
||
381 | |||
382 | if (preg_match('/^administrator_(.*)$/', $translation, $matches)) |
||
383 | { |
||
384 | $administrator[] = $matches[1]; |
||
385 | } |
||
386 | } |
||
387 | |||
388 | // Add the site language files |
||
389 | View Code Duplication | if (count($site)) |
|
390 | { |
||
391 | $siteXml = $dom->createElement('site'); |
||
392 | |||
393 | foreach ($site as $translation) |
||
394 | { |
||
395 | $fileElement = $dom->createElement('filename', $translation . '.ini'); |
||
396 | $siteXml->appendChild($fileElement); |
||
397 | } |
||
398 | |||
399 | $packageXml->appendChild($siteXml); |
||
400 | } |
||
401 | |||
402 | // Add the administrator language files |
||
403 | View Code Duplication | if (count($administrator)) |
|
404 | { |
||
405 | $adminXml = $dom->createElement('administrator'); |
||
406 | |||
407 | foreach ($administrator as $translation) |
||
408 | { |
||
409 | $fileElement = $dom->createElement('filename', $translation . '.ini'); |
||
410 | $adminXml->appendChild($fileElement); |
||
411 | } |
||
412 | |||
413 | $packageXml->appendChild($adminXml); |
||
414 | } |
||
415 | |||
416 | $dom->appendChild($packageXml); |
||
417 | |||
418 | // Set FTP credentials, if given. |
||
419 | JClientHelper::setCredentialsFromRequest('ftp'); |
||
420 | $ftp = JClientHelper::getCredentials('ftp'); |
||
421 | |||
422 | // Try to make the file writeable. |
||
423 | View Code Duplication | if (JFile::exists($path) && !$ftp['enabled'] && JPath::isOwner($path) && !JPath::setPermissions($path, '0644')) |
|
424 | { |
||
425 | $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_WRITABLE', $path)); |
||
426 | |||
427 | return false; |
||
428 | } |
||
429 | |||
430 | // Make the XML look pretty |
||
431 | $dom->formatOutput = true; |
||
432 | $formattedXML = $dom->saveXML(); |
||
433 | |||
434 | $return = JFile::write($path, $formattedXML); |
||
435 | |||
436 | // Try to make the file unwriteable. |
||
437 | View Code Duplication | if (!$ftp['enabled'] && JPath::isOwner($path) && !JPath::setPermissions($path, '0444')) |
|
438 | { |
||
439 | $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_UNWRITABLE', $path)); |
||
440 | |||
441 | return false; |
||
442 | } |
||
443 | elseif (!$return) |
||
444 | { |
||
445 | $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_FILESAVE', $path)); |
||
446 | |||
447 | return false; |
||
448 | } |
||
449 | } |
||
450 | |||
451 | /** @TODO: Check ftp code |
||
452 | // Save the title and the description in the language file |
||
453 | $translation_path = LocaliseHelper::findTranslationPath($client, JFactory::getLanguage()->getTag(), $manifest); |
||
454 | $translation_id = LocaliseHelper::getFileId($translation_path); |
||
455 | $translation_model = JModelLegacy::getInstance('Translation', 'LocaliseModel', array('ignore_request' => true)); |
||
456 | |||
457 | if ($translation_model->checkout($translation_id)) |
||
458 | { |
||
459 | $translation_model->setState('translation.path', $translation_path); |
||
460 | $translation_model->setState('translation.client', $client); |
||
461 | $translation = $translation_model->getItem(); |
||
462 | $sections = LocaliseHelper::parseSections($translation_path); |
||
463 | } |
||
464 | else |
||
465 | { |
||
466 | } |
||
467 | |||
468 | $text = ''; |
||
469 | //$text .= strtoupper($title) . '="' . str_replace('"', '"_QQ_"', $data['title']) . "\"\n"; |
||
470 | //$text .= strtoupper($description) . '="' . str_replace('"', '"_QQ_"', $data['description']) . "\"\n"; |
||
471 | //$tag = JFactory::getLanguage()->getTag(); |
||
472 | //$languagePath = JPATH_SITE . "/language/$tag/$tag.$manifest.ini"; |
||
473 | |||
474 | // Try to make the file writeable. |
||
475 | if (!$ftp['enabled'] && JPath::isOwner($languagePath) && !JPath::setPermissions($languagePath, '0644')) |
||
476 | { |
||
477 | $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_WRITABLE', $languagePath)); |
||
478 | |||
479 | return false; |
||
480 | } |
||
481 | |||
482 | //$return = JFile::write($languagePath, $text); |
||
483 | |||
484 | // Try to make the file unwriteable. |
||
485 | if (!$ftp['enabled'] && JPath::isOwner($languagePath) && !JPath::setPermissions($languagePath, '0444')) |
||
486 | { |
||
487 | $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_UNWRITABLE', $languagePath)); |
||
488 | |||
489 | return false; |
||
490 | } |
||
491 | elseif (!$return) |
||
492 | { |
||
493 | $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_FILESAVE', $languagePath)); |
||
494 | |||
495 | return false; |
||
496 | } |
||
497 | |||
498 | */ |
||
499 | View Code Duplication | if ($path == $oldpath) |
|
500 | { |
||
501 | $id = LocaliseHelper::getFileId($path); |
||
502 | $this->setState('packagefile.id', $id); |
||
503 | |||
504 | // Bind the rules. |
||
505 | $table = $this->getTable(); |
||
506 | $table->load($id); |
||
507 | } |
||
508 | else |
||
509 | { |
||
510 | $table = $this->getTable(); |
||
511 | |||
512 | if (!$table->delete((int) $originalId)) |
||
513 | { |
||
514 | $this->setError($table->getError()); |
||
515 | |||
516 | return false; |
||
517 | } |
||
518 | |||
519 | $table->store(); |
||
520 | |||
521 | $id = LocaliseHelper::getFileId($path); |
||
522 | $this->setState('packagefile.id', $id); |
||
523 | $app->setUserState('com_localise.edit.packagefile.id', $id); |
||
524 | } |
||
525 | |||
526 | if (isset($data['rules'])) |
||
527 | { |
||
528 | $rules = new JAccessRules($data['rules']); |
||
529 | $table->setRules($rules); |
||
530 | } |
||
531 | |||
532 | // Check the data. |
||
533 | if (!$table->check()) |
||
534 | { |
||
535 | $this->setError($table->getError()); |
||
536 | |||
537 | return false; |
||
538 | } |
||
539 | |||
540 | // Store the data. |
||
541 | if (!$table->store()) |
||
542 | { |
||
543 | $this->setError($table->getError()); |
||
544 | |||
545 | return false; |
||
546 | } |
||
547 | |||
548 | // Delete the older file and redirect |
||
549 | View Code Duplication | if ($path !== $oldpath && file_exists($oldpath)) |
|
550 | { |
||
551 | if (!JFile::delete($oldpath)) |
||
552 | { |
||
553 | $app->enqueueMessage(JText::_('COM_LOCALISE_ERROR_OLDFILE_REMOVE'), 'notice'); |
||
554 | } |
||
555 | |||
556 | $task = JFactory::getApplication()->input->get('task'); |
||
557 | |||
558 | if ($task == 'save') |
||
559 | { |
||
560 | $app->redirect(JRoute::_('index.php?option=com_localise&view=packages', false)); |
||
561 | } |
||
562 | else |
||
563 | { |
||
564 | // Redirect to the new $id as name has changed |
||
565 | $app->redirect(JRoute::_('index.php?option=com_localise&view=packagefile&layout=edit&id=' . $this->getState('packagefile.id'), false)); |
||
566 | } |
||
567 | } |
||
568 | |||
569 | $this->cleanCache(); |
||
570 | |||
571 | return true; |
||
572 | } |
||
573 | |||
841 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.