Conditions | 24 |
Paths | 422 |
Total Lines | 259 |
Lines | 195 |
Ratio | 75.29 % |
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 |
||
581 | public function download($data) |
||
582 | { |
||
583 | // The data could potentially be loaded from the file with $this->getItem() instead of using directly the data from the post |
||
584 | $app = JFactory::getApplication(); |
||
585 | |||
586 | // Prevent generating and downloading Master package |
||
587 | View Code Duplication | if (strpos($data['name'], 'master_') !== false) |
|
588 | { |
||
589 | $app->enqueueMessage(JText::sprintf('COM_LOCALISE_ERROR_MASTER_PACKAGE_DOWNLOAD_FORBIDDEN', $data['name']), 'warning'); |
||
590 | $app->redirect(JRoute::_('index.php?option=com_localise&view=packagefile&layout=edit&id=' . $this->getState('packagefile.id'), false)); |
||
591 | |||
592 | return false; |
||
593 | } |
||
594 | |||
595 | $administrator = array(); |
||
596 | $site = array(); |
||
597 | $msg = null; |
||
598 | |||
599 | // Delete old files |
||
600 | $delete = JFolder::files(JPATH_ROOT . '/tmp/', 'com_localise_', false, true); |
||
601 | |||
602 | View Code Duplication | if (!empty($delete)) |
|
603 | { |
||
604 | if (!JFile::delete($delete)) |
||
605 | { |
||
606 | // JFile::delete throws an error |
||
607 | $this->setError(JText::_('COM_LOCALISE_ERROR_EXPORT_ZIPDELETE')); |
||
608 | |||
609 | return false; |
||
610 | } |
||
611 | } |
||
612 | |||
613 | View Code Duplication | foreach ($data['translations'] as $translation) |
|
614 | { |
||
615 | if (preg_match('/^site_(.*)$/', $translation, $matches)) |
||
616 | { |
||
617 | $site[] = $matches[1]; |
||
618 | } |
||
619 | |||
620 | if (preg_match('/^administrator_(.*)$/', $translation, $matches)) |
||
621 | { |
||
622 | $administrator[] = $matches[1]; |
||
623 | } |
||
624 | } |
||
625 | |||
626 | $parts = explode('.', $data['version']); |
||
627 | $small_version = implode('.', array($parts[0],$parts[1])); |
||
628 | |||
629 | // Prepare text to save for the xml package description |
||
630 | $text = ''; |
||
631 | $text .= '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; |
||
632 | $text .= '<extension type="file" version="' . $small_version . '" method="upgrade">' . "\n"; |
||
633 | $text .= "\t" . '<name>' . $data['name'] . $data['language'] . '</name>' . "\n"; |
||
634 | $text .= "\t" . '<version>' . $data['version'] . '.' . $data['packversion'] . '</version>' . "\n"; |
||
635 | $text .= "\t" . '<creationDate>' . date('d/m/Y') . '</creationDate>' . "\n"; |
||
636 | $text .= "\t" . '<author>' . $data['author'] . '</author>' . "\n"; |
||
637 | $text .= "\t" . '<authorEmail>' . $data['authoremail'] . '</authorEmail>' . "\n"; |
||
638 | $text .= "\t" . '<authorUrl>' . $data['authorurl'] . '</authorUrl>' . "\n"; |
||
639 | $text .= "\t" . '<copyright>' . $data['copyright'] . '</copyright>' . "\n"; |
||
640 | $text .= "\t" . '<license>' . $data['license'] . '</license>' . "\n"; |
||
641 | $text .= "\t" . '<packager>' . $data['packager'] . '</packager>' . "\n"; |
||
642 | $text .= "\t" . '<packagerurl>' . $data['packagerurl'] . '</packagerurl>' . "\n"; |
||
643 | $text .= "\t" . '<description><![CDATA[' . $data['description'] . ']]></description>' . "\n"; |
||
644 | $text .= "\t" . '<fileset>' . "\n"; |
||
645 | |||
646 | View Code Duplication | if (count($site)) |
|
647 | { |
||
648 | $text .= "\t\t" . '<files '; |
||
649 | $text .= 'folder="site/' . $data['language'] . '"'; |
||
650 | $text .= ' target="language/' . $data['language'] . '">' . "\n"; |
||
651 | $site_package_files = array(); |
||
652 | |||
653 | // $site_package_zip_path = JPATH_ROOT . '/tmp/' . uniqid('com_localise_') . '.zip'; |
||
654 | |||
655 | foreach ($site as $translation) |
||
656 | { |
||
657 | $path = LocaliseHelper::findTranslationPath($client = 'site', $tag = $data['language'], $filename = $translation); |
||
658 | |||
659 | if (JFile::exists($path)) |
||
660 | { |
||
661 | $file_data = file_get_contents($path); |
||
662 | } |
||
663 | |||
664 | if (JFile::exists($path) && !empty($file_data)) |
||
665 | { |
||
666 | $text .= "\t\t\t" . '<filename>' . $data['language'] . '.' . $translation . '.ini</filename>' . "\n"; |
||
667 | $site_package_files[] = array('name' => $data['language'] . '.' . $translation . '.ini','data' => $file_data); |
||
668 | } |
||
669 | else |
||
670 | { |
||
671 | $msg .= JText::sprintf('COM_LOCALISE_FILE_NOT_TRANSLATED', $data['language'] . '.' . $translation . '.ini', JText::_('JSITE')); |
||
672 | } |
||
673 | } |
||
674 | |||
675 | /** |
||
676 | $site_txt .= "\t\t".'<filename file="meta">install.xml</filename>' . "\n"; |
||
677 | $site_txt .= "\t\t".'<filename file="meta">' . $data['language'] . '.xml</filename>' . "\n"; |
||
678 | $site_txt .= "\t".'</files>' . "\n"; |
||
679 | $site_txt .= "\t".'<params />' . "\n"; |
||
680 | $site_txt .= "\t".'</extension>' . "\n"; |
||
681 | $site_package_files[] = array('name'=>'install.xml','data'=>$site_txt); |
||
682 | $language_data = file_get_contents(JPATH_ROOT . '/language/' . $data['language'] . '/' . $data['language'] . '.xml'); |
||
683 | $site_package_files[] = array('name' => $data['language'] . '.xml','data'=>$language_data); |
||
684 | $language_data = file_get_contents(JPATH_ROOT . '/language/' . $data['language'] . '/' . $data['language'] . '.localise.php'); |
||
685 | $site_package_files[] = array('name' => $data['language'] . '.localise.php','data' => $language_data); |
||
686 | |||
687 | $site_zip_path = JPATH_ROOT . '/tmp/' . uniqid('com_localise_') . '.zip'; |
||
688 | if (!$packager = JArchive::getAdapter('zip')) |
||
689 | { |
||
690 | $this->setError(JText::_('COM_LOCALISE_ERROR_EXPORT_ADAPTER')); |
||
691 | |||
692 | return false; |
||
693 | } |
||
694 | else |
||
695 | { |
||
696 | if (!$packager->create($site_zip_path, $site_package_files)) |
||
697 | { |
||
698 | $this->setError(JText::_('COM_LOCALISE_ERROR_EXPORT_ZIPCREATE')); |
||
699 | |||
700 | return false; |
||
701 | } |
||
702 | } |
||
703 | */ |
||
704 | |||
705 | $text .= "\t\t" . '</files>' . "\n"; |
||
706 | |||
707 | if ($msg) |
||
708 | { |
||
709 | $msg .= '<p>...</p>'; |
||
710 | } |
||
711 | |||
712 | foreach ($site_package_files as $file) |
||
713 | { |
||
714 | $main_package_files[] = array('name' => 'site/' . $data['language'] . '/' . $file['name'], 'data' => $file['data']); |
||
715 | } |
||
716 | } |
||
717 | |||
718 | View Code Duplication | if (count($administrator)) |
|
719 | { |
||
720 | $text .= "\t\t" . '<files '; |
||
721 | $text .= 'folder="admin/' . $data['language'] . '"'; |
||
722 | $text .= ' target="administrator/language/' . $data['language'] . '">' . "\n"; |
||
723 | |||
724 | $admin_package_files = array(); |
||
725 | |||
726 | foreach ($administrator as $translation) |
||
727 | { |
||
728 | $path = LocaliseHelper::findTranslationPath($client = 'administrator', $tag = $data['language'], $filename = $translation); |
||
729 | |||
730 | if (JFile::exists($path)) |
||
731 | { |
||
732 | $file_data = file_get_contents($path); |
||
733 | } |
||
734 | |||
735 | if (JFile::exists($path) && !empty($file_data)) |
||
736 | { |
||
737 | $text .= "\t\t\t" . '<filename>' . $data['language'] . '.' . $translation . '.ini</filename>' . "\n"; |
||
738 | $admin_package_files[] = array('name' => $data['language'] . '.' . $translation . '.ini','data' => $file_data); |
||
739 | } |
||
740 | else |
||
741 | { |
||
742 | $msg .= JText::sprintf('COM_LOCALISE_FILE_NOT_TRANSLATED', $data['language'] . '.' . $translation . '.ini', JText::_('JADMINISTRATOR')); |
||
743 | } |
||
744 | } |
||
745 | |||
746 | /** |
||
747 | $admin_txt .= "\t\t".'<filename file="meta">install.xml</filename>' . "\n"; |
||
748 | $admin_txt .= "\t\t".'<filename file="meta">' . $data['language'].'.xml</filename>' . "\n"; |
||
749 | $admin_txt .= "\t".'</files>' . "\n"; |
||
750 | $admin_txt .= "\t".'<params />' . "\n"; |
||
751 | $admin_txt .= "\t".'</extension>' . "\n"; |
||
752 | $admin_package_files[] = array('name'=>'install.xml','data'=>$admin_txt); |
||
753 | $language_data = file_get_contents(JPATH_ROOT . '/administrator/language/' . $data['language'] . '/' . $data['language'] . '.xml'); |
||
754 | $admin_package_files[] = array('name'=>$data['language'] . '.xml','data' => $language_data); |
||
755 | $language_data = file_get_contents(JPATH_ROOT . '/administrator/language/' . $data['language'] . '/' . $data['language'] . '.localise.php'); |
||
756 | $admin_package_files[] = array('name'=>$data['language'] . '.localise.php','data' => $language_data); |
||
757 | |||
758 | |||
759 | $admin_zip_path = JPATH_ROOT . '/tmp/' . uniqid('com_localise_') . '.zip'; |
||
760 | if (!$packager = JArchive::getAdapter('zip')) |
||
761 | { |
||
762 | $this->setError(JText::_('COM_LOCALISE_ERROR_EXPORT_ADAPTER')); |
||
763 | |||
764 | return false; |
||
765 | } |
||
766 | else |
||
767 | { |
||
768 | if (!$packager->create($admin_zip_path, $admin_package_files)) |
||
769 | { |
||
770 | $this->setError(JText::_('COM_LOCALISE_ERROR_EXPORT_ZIPCREATE')); |
||
771 | |||
772 | return false; |
||
773 | } |
||
774 | } |
||
775 | */ |
||
776 | $text .= "\t\t" . '</files>' . "\n"; |
||
777 | |||
778 | foreach ($admin_package_files as $file) |
||
779 | { |
||
780 | $main_package_files[] = array('name' => 'admin/' . $data['language'] . '/' . $file['name'], 'data' => $file['data']); |
||
781 | } |
||
782 | } |
||
783 | |||
784 | View Code Duplication | if ($msg) |
|
785 | { |
||
786 | $msg .= '<p>...</p>'; |
||
787 | $msg .= JText::_('COM_LOCALISE_UNTRANSLATED'); |
||
788 | $app->enqueueMessage($msg, 'error'); |
||
789 | $app->redirect(JRoute::_('index.php?option=com_localise&view=packagefile&layout=edit&id=' . $this->getState('packagefile.id'), false)); |
||
790 | |||
791 | return false; |
||
792 | } |
||
793 | |||
794 | $text .= "\t" . '</fileset>' . "\n"; |
||
795 | |||
796 | View Code Duplication | if (!empty($data['serverurl'])) |
|
797 | { |
||
798 | $text .= "\t" . '<updateservers>' . "\n"; |
||
799 | $text .= "\t\t" . '<server type="collection" priority="1" name="' . $data['servername'] . '">' . $data['serverurl'] . '</server>' . "\n"; |
||
800 | $text .= "\t" . '</updateservers>' . "\n"; |
||
801 | } |
||
802 | |||
803 | $text .= '</extension>' . "\n"; |
||
804 | |||
805 | $main_package_files[] = array('name' => $data['name'] . $data['language'] . '.xml', 'data' => $text); |
||
806 | |||
807 | $ziproot = JPATH_ROOT . '/tmp/' . uniqid('com_localise_main_') . '.zip'; |
||
808 | |||
809 | // Run the packager |
||
810 | View Code Duplication | if (!$packager = JArchive::getAdapter('zip')) |
|
811 | { |
||
812 | $this->setError(JText::_('COM_LOCALISE_ERROR_EXPORT_ADAPTER')); |
||
813 | |||
814 | return false; |
||
815 | } |
||
816 | else |
||
817 | { |
||
818 | if (!$packager->create($ziproot, $main_package_files)) |
||
819 | { |
||
820 | $this->setError(JText::_('COM_LOCALISE_ERROR_EXPORT_ZIPCREATE')); |
||
821 | |||
822 | return false; |
||
823 | } |
||
824 | } |
||
825 | |||
826 | ob_clean(); |
||
827 | $zipdata = file_get_contents($ziproot); |
||
828 | header("Expires: 0"); |
||
829 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
||
830 | header('Content-Type: application/zip'); |
||
831 | header('Content-Disposition: attachment; filename="' |
||
832 | . $data['name'] . '_' . $data['language'] . '_' . $data['version'] . 'v' . $data['packversion'] . '.zip"'); |
||
833 | header('Content-Length: ' . strlen($zipdata)); |
||
834 | header("Cache-Control: maxage=1"); |
||
835 | header("Pragma: public"); |
||
836 | header("Content-Transfer-Encoding: binary"); |
||
837 | echo $zipdata; |
||
838 | exit; |
||
839 | } |
||
840 | } |
||
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.