Conditions | 16 |
Paths | 61 |
Total Lines | 112 |
Code Lines | 73 |
Lines | 8 |
Ratio | 7.14 % |
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 |
||
10 | public static function entryToXml($parentField, $entry, $entrySectionHandle, $entryFields) |
||
11 | { |
||
12 | $date = new DateTime(); |
||
13 | $params = array( |
||
14 | 'today' => $date->format('Y-m-d'), |
||
15 | 'current-time' => $date->format('H:i'), |
||
16 | 'this-year' => $date->format('Y'), |
||
17 | 'this-month' => $date->format('m'), |
||
18 | 'this-day' => $date->format('d'), |
||
19 | 'timezone' => $date->format('P'), |
||
20 | 'website-name' => Symphony::Configuration()->get('sitename', 'general'), |
||
21 | 'root' => URL, |
||
22 | 'workspace' => URL . '/workspace', |
||
23 | 'http-host' => HTTP_HOST |
||
24 | ); |
||
25 | $entryData = $entry->getData(); |
||
26 | $includedElements = FieldEntry_relationship::parseElements($parentField); |
||
27 | |||
28 | $xslFilePath = WORKSPACE . '/er-templates/' . $entrySectionHandle . '.xsl'; |
||
29 | if (!empty($entryData) && !!@file_exists($xslFilePath)) { |
||
30 | $xmlData = new XMLElement('data'); |
||
31 | $xmlData->setIncludeHeader(true); |
||
32 | $xml = new XMLElement('entry'); |
||
33 | $xml->setAttribute('id', $entryId); |
||
|
|||
34 | $xmlData->appendChild(self::getXmlParams($params)); |
||
35 | $xmlData->appendChild($xml); |
||
36 | foreach ($entryData as $fieldId => $data) { |
||
37 | $filteredData = array_filter($data, function ($value) { |
||
38 | return $value != null; |
||
39 | }); |
||
40 | if (empty($filteredData)) { |
||
41 | continue; |
||
42 | } |
||
43 | $field = $entryFields[$fieldId]; |
||
44 | $fieldName = $field->get('element_name'); |
||
45 | $fieldIncludedElement = $includedElements[$entrySectionHandle]; |
||
46 | |||
47 | try { |
||
48 | if (FieldEntry_relationship::isFieldIncluded($fieldName, $fieldIncludedElement)) { |
||
49 | $parentIncludableElement = FieldEntry_relationship::getSectionElementName($fieldName, $fieldIncludedElement); |
||
50 | $parentIncludableElementMode = FieldEntry_relationship::extractMode($fieldName, $parentIncludableElement); |
||
51 | |||
52 | // Special treatments for ERF |
||
53 | if ($field instanceof FieldEntry_relationship) { |
||
54 | // Increment recursive level |
||
55 | $field->recursiveLevel = $recursiveLevel + 1; |
||
56 | $field->recursiveDeepness = $deepness; |
||
57 | } |
||
58 | |||
59 | View Code Duplication | if ($parentIncludableElementMode == null) { |
|
60 | $submodes = array_map(function ($fieldIncludableElement) use ($fieldName) { |
||
61 | return FieldEntry_relationship::extractMode($fieldName, $fieldIncludableElement); |
||
62 | }, $field->fetchIncludableElements()); |
||
63 | } |
||
64 | else { |
||
65 | $submodes = array($parentIncludableElementMode); |
||
66 | } |
||
67 | |||
68 | foreach ($submodes as $submode) { |
||
69 | $field->appendFormattedElement($xml, $filteredData, false, $submode, $entryId); |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | catch (Exception $ex) { |
||
74 | $xml->appendChild(new XMLElement('error', $ex->getMessage() . ' on ' . $ex->getLine() . ' of file ' . $ex->getFile())); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | $indent = false; |
||
79 | $mode = $parentField->get('mode'); |
||
80 | if (isset($_REQUEST['debug'])) { |
||
81 | $mode = 'debug'; |
||
82 | } |
||
83 | if ($mode == 'debug') { |
||
84 | $indent = true; |
||
85 | } |
||
86 | $xmlMode = empty($mode) ? '' : 'mode="' . $mode . '"'; |
||
87 | $xmlString = $xmlData->generate($indent, 0); |
||
88 | $xsl = '<?xml version="1.0" encoding="UTF-8"?> |
||
89 | <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
||
90 | <xsl:import href="' . str_replace('\\', '/', $xslFilePath) . '"/> |
||
91 | <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="no" /> |
||
92 | <xsl:template match="/"> |
||
93 | <xsl:apply-templates select="/data" ' . $xmlMode . ' /> |
||
94 | </xsl:template> |
||
95 | <xsl:template match="/data" ' . $xmlMode . '> |
||
96 | <xsl:apply-templates select="entry" ' . $xmlMode . ' /> |
||
97 | </xsl:template> |
||
98 | <xsl:template match="/data" mode="debug"> |
||
99 | <xsl:copy-of select="/" /> |
||
100 | </xsl:template> |
||
101 | </xsl:stylesheet>'; |
||
102 | $xslt = new XsltProcess(); |
||
103 | $result = $xslt->process($xmlString, $xsl, $params); |
||
104 | |||
105 | if ($mode == 'debug') { |
||
106 | $result = '<pre><code>' . |
||
107 | str_replace('<', '<', str_replace('>', '>', $xmlString)) . |
||
108 | '</code></pre>'; |
||
109 | } |
||
110 | |||
111 | if ($xslt->isErrors()) { |
||
112 | $error = $xslt->getError(); |
||
113 | $result = $error[1]['message']; |
||
114 | } |
||
115 | |||
116 | if (General::strlen($result) > 0) { |
||
117 | return new XMLElement('div', $result, array('class' => 'content')); |
||
118 | } |
||
119 | } |
||
120 | return null; |
||
121 | } |
||
122 | |||
130 | } |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.