Completed
Push — master ( 41d4aa...7dfe14 )
by Damian
02:52
created

SiteTreeLinkTracking_Parser::process()   D

Complexity

Conditions 13
Paths 15

Size

Total Lines 85
Code Lines 51

Duplication

Lines 22
Ratio 25.88 %
Metric Value
dl 22
loc 85
rs 4.9922
cc 13
eloc 51
nc 15
nop 1

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * @package cms
5
 * @subpackage model
6
 */
7
8
/**
9
 * Adds tracking of links in any HTMLText fields which reference SiteTree or File items.
10
 *
11
 * Attaching this to any DataObject will add four fields which contain all links to SiteTree and File items
12
 * referenced in any HTMLText fields, and two booleans to indicate if there are any broken links. Call
13
 * augmentSyncLinkTracking to update those fields with any changes to those fields.
14
 *
15
 * @property SiteTree $owner
16
 *
17
 * @property bool $HasBrokenFile
18
 * @property bool $HasBrokenLink
19
 *
20
 * @method ManyManyList LinkTracking() List of site pages linked on this page.
21
 * @method ManyManyList ImageTracking() List of Images linked on this page.
22
 */
23
class SiteTreeLinkTracking extends DataExtension {
24
25
	public $parser;
26
27
	private static $dependencies = array(
28
		'parser' => '%$SiteTreeLinkTracking_Parser'
29
	);
30
31
	private static $db = array(
32
		"HasBrokenFile" => "Boolean",
33
		"HasBrokenLink" => "Boolean"
34
	);
35
36
	private static $many_many = array(
37
		"LinkTracking" => "SiteTree",
38
		"ImageTracking" => "File"
39
	);
40
41
	private static $many_many_extraFields = array(
42
		"LinkTracking" => array("FieldName" => "Varchar"),
43
		"ImageTracking" => array("FieldName" => "Varchar")
44
	);
45
46
	/**
47
	 * Scrape the content of a field to detect anly links to local SiteTree pages or files
48
	 *
49
	 * @param string $fieldName The name of the field on {@link @owner} to scrape
50
	 */
51
	public function trackLinksInField($fieldName) {
52
		$record = $this->owner;
53
54
		$linkedPages = array();
55
		$linkedFiles = array();
56
57
		$htmlValue = Injector::inst()->create('HTMLValue', $record->$fieldName);
58
		$links = $this->parser->process($htmlValue);
59
60
		// Highlight broken links in the content.
61
		foreach ($links as $link) {
62
			$classStr = trim($link['DOMReference']->getAttribute('class'));
63
			if (!$classStr) {
64
				$classes = array();
65
			} else {
66
				$classes = explode(' ', $classStr);
67
			}
68
69
			// Add or remove the broken class from the link, depending on the link status.
70
			if ($link['Broken']) {
71
				$classes = array_unique(array_merge($classes, array('ss-broken')));
72
			} else {
73
				$classes = array_diff($classes, array('ss-broken'));
74
			}
75
76
			if (!empty($classes)) {
77
				$link['DOMReference']->setAttribute('class', implode(' ', $classes));
78
			} else {
79
				$link['DOMReference']->removeAttribute('class');
80
			}
81
		}
82
		$record->$fieldName = $htmlValue->getContent();
83
84
		// Populate link tracking for internal links & links to asset files.
85
		foreach ($links as $link) {
86
			switch ($link['Type']) {
87
				case 'sitetree':
88
					if ($link['Broken']) {
89
						$record->HasBrokenLink = true;
0 ignored issues
show
Documentation introduced by
The property HasBrokenLink does not exist on object<SiteTree>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
					} else {
91
						$linkedPages[] = $link['Target'];
92
					}
93
					break;
94
95
				case 'file':
96
					if ($link['Broken']) {
97
						$record->HasBrokenFile = true;
0 ignored issues
show
Documentation introduced by
The property HasBrokenFile does not exist on object<SiteTree>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
98
					} else {
99
						$linkedFiles[] = $link['Target'];
100
					}
101
					break;
102
103
				default:
104
					if ($link['Broken']) {
105
						$record->HasBrokenLink = true;
0 ignored issues
show
Documentation introduced by
The property HasBrokenLink does not exist on object<SiteTree>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
106
					}
107
					break;
108
			}
109
		}
110
111
		// Add file tracking for image references
112
		if($images = $htmlValue->getElementsByTagName('img')) foreach($images as $img) {
113
			// {@see HtmlEditorField} for data-fileid source
114
			$fileID = $img->getAttribute('data-fileid');
115
			if(!$fileID) {
116
				continue;
117
			}
118
119
			// Assuming a local file is linked, check if it's valid
120
			if($image = File::get()->byID($fileID)) {
121
				$linkedFiles[] = $image->ID;
122
			} else {
123
				$record->HasBrokenFile = true;
0 ignored issues
show
Documentation introduced by
The property HasBrokenFile does not exist on object<SiteTree>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
124
			}
125
		}
126
127
		// Update the "LinkTracking" many_many
128 View Code Duplication
		if($record->ID && $record->manyManyComponent('LinkTracking') && ($tracker = $record->LinkTracking())) {
0 ignored issues
show
Bug introduced by
The method LinkTracking() does not exist on SiteTree. Did you maybe mean syncLinkTracking()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
129
			$tracker->removeByFilter(array(
130
				sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey())
131
					=> array($fieldName, $record->ID)
132
			));
133
134
			if($linkedPages) foreach($linkedPages as $item) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $linkedPages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
135
				$tracker->add($item, array('FieldName' => $fieldName));
136
			}
137
		}
138
139
		// Update the "ImageTracking" many_many
140 View Code Duplication
		if($record->ID && $record->manyManyComponent('ImageTracking') && ($tracker = $record->ImageTracking())) {
0 ignored issues
show
Documentation Bug introduced by
The method ImageTracking does not exist on object<SiteTree>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
141
			$tracker->removeByFilter(array(
142
				sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey())
143
					=> array($fieldName, $record->ID)
144
			));
145
146
			if($linkedFiles) foreach($linkedFiles as $item) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $linkedFiles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
147
				$tracker->add($item, array('FieldName' => $fieldName));
148
			}
149
		}
150
	}
151
152
	/**
153
	 * Find HTMLText fields on {@link owner} to scrape for links that need tracking
154
	 */
155
	public function augmentSyncLinkTracking() {
156
		// Reset boolean broken flags
157
		$this->owner->HasBrokenLink = false;
0 ignored issues
show
Documentation introduced by
The property HasBrokenLink does not exist on object<SiteTree>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
158
		$this->owner->HasBrokenFile = false;
0 ignored issues
show
Documentation introduced by
The property HasBrokenFile does not exist on object<SiteTree>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
159
160
		// Build a list of HTMLText fields
161
		$allFields = $this->owner->db();
162
		$htmlFields = array();
163
		foreach($allFields as $field => $fieldSpec) {
0 ignored issues
show
Bug introduced by
The expression $allFields of type array|string|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
164
			if(preg_match('/([^(]+)/', $fieldSpec, $matches)) {
165
				$class = $matches[0];
166
				if(class_exists($class)){
167
					if($class == 'HTMLText' || is_subclass_of($class, 'HTMLText')) $htmlFields[] = $field;
168
				}
169
			}
170
		}
171
172
		foreach($htmlFields as $field) $this->trackLinksInField($field);
173
	}
174
}
175
176
/**
177
 * A helper object for extracting information about links.
178
 */
179
class SiteTreeLinkTracking_Parser {
180
181
	/**
182
	 * Finds the links that are of interest for the link tracking automation. Checks for brokenness and attaches
183
	 * extracted metadata so consumers can decide what to do with the DOM element (provided as DOMReference).
184
	 *
185
	 * @param SS_HTMLValue $htmlValue Object to parse the links from.
186
	 * @return array Associative array containing found links with the following field layout:
187
	 *		Type: string, name of the link type
188
	 *		Target: any, a reference to the target object, depends on the Type
189
	 *		Anchor: string, anchor part of the link
190
	 *		DOMReference: DOMElement, reference to the link to apply changes.
191
	 *		Broken: boolean, a flag highlighting whether the link should be treated as broken.
192
	 */
193
	public function process(SS_HTMLValue $htmlValue) {
194
		$results = array();
195
196
		$links = $htmlValue->getElementsByTagName('a');
197
		if(!$links) return $results;
198
199
		foreach($links as $link) {
200
			if (!$link->hasAttribute('href')) continue;
201
202
			$href = Director::makeRelative($link->getAttribute('href'));
203
204
			// Definitely broken links.
205
			if($href == '' || $href[0] == '/') {
206
				$results[] = array(
207
					'Type' => 'broken',
208
					'Target' => null,
209
					'Anchor' => null,
210
					'DOMReference' => $link,
211
					'Broken' => true
212
				);
213
214
				continue;
215
			}
216
217
			// Link to a page on this site.
218
			$matches = array();
219
			if(preg_match('/\[sitetree_link(?:\s*|%20|,)?id=([0-9]+)\](#(.*))?/i', $href, $matches)) {
220
				$page = DataObject::get_by_id('SiteTree', $matches[1]);
221
				$broken = false;
222
223
				if (!$page) {
224
					// Page doesn't exist.
225
					$broken = true;
226
				} else if (!empty($matches[3])) {
227
					$anchor = preg_quote($matches[3], '/');
228
229
					if (!preg_match("/(name|id)=\"{$anchor}\"/", $page->Content)) {
230
						// Broken anchor on the target page.
231
						$broken = true;
232
					}
233
				}
234
235
				$results[] = array(
236
					'Type' => 'sitetree',
237
					'Target' => $matches[1],
238
					'Anchor' => empty($matches[3]) ? null : $matches[3],
239
					'DOMReference' => $link,
240
					'Broken' => $broken
241
				);
242
243
				continue;
244
			}
245
246
			// Link to a file on this site.
247
			$matches = array();
248 View Code Duplication
			if(preg_match('/\[file_link(?:\s*|%20|,)?id=([0-9]+)\]/i', $href, $matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
249
				$results[] = array(
250
					'Type' => 'file',
251
					'Target' => $matches[1],
252
					'Anchor' => null,
253
					'DOMReference' => $link,
254
					'Broken' => !DataObject::get_by_id('File', $matches[1])
255
				);
256
257
				continue;
258
			}
259
260
			// Local anchor.
261
			$matches = array();
262 View Code Duplication
			if(preg_match('/^#(.*)/i', $href, $matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
263
				$results[] = array(
264
					'Type' => 'localanchor',
265
					'Target' => null,
266
					'Anchor' => $matches[1],
267
					'DOMReference' => $link,
268
					'Broken' => !preg_match("#(name|id)=\"{$matches[1]}\"#", $htmlValue->getContent())
269
				);
270
271
				continue;
272
			}
273
274
		}
275
276
		return $results;
277
	}
278
279
}
280