Completed
Push — master ( 952f49...c4dcc1 )
by Robbie
01:49
created

MarkdownTextareaField::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
class MarkdownTextareaField extends TextareaField {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
	/**
6
	 * Define the actions allowed on this field
7
	 * @var array
8
	 */
9
	private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
		'preview',
11
		'parse'
12
	);
13
14
	/**
15
	 * @var int Visible number of text lines.
16
	 * Default on TextareaField is too small
17
	 */
18
	protected $rows = 15;
19
20
	/**
21
	 * Toggle rendering markdown with extra syntax enabled.
22
	 * @link http://michelf.ca/projects/php-markdown/extra
23
	 * @var boolean
24
	 */
25
	protected $enable_extra = false;
26
27
	/**
28
     * Returns the field holder used by templates
29
     * @return string HTML to be used
30
     */
31
    public function FieldHolder($properties = array()) {
32
		
33
		$this->extraClasses['stacked'] = 'stacked';
34
35
		Requirements::css('markdowntextareafield/templates/css/styles.css');
36
37
		Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
38
		Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
39
		Requirements::javascript('markdowntextareafield/thirdparty/textinputs_jquery.js');
40
		Requirements::javascript('markdowntextareafield/templates/javascript/script.js');
41
42
		return parent::FieldHolder($properties);
43
    }
44
45
	/**
46
	 * Turn on extra syntax support
47
	 * @return MarkdownTextareaField
48
	 */
49
	public function enableExtra() {
50
		$this->enable_extra = true;
51
		return $this;
52
	}
53
54
55
	/**
56
	 * Body for the preview iframe with just the typography styles included
57
	 * @return string html
0 ignored issues
show
Documentation introduced by
Should the return type not be HTMLText?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
58
	 */
59
	public function preview() {
60
		Requirements::clear();
61
		// Should contain text styles of the page by Silverstripe theme conventions.
62
		Requirements::css('themes/' . Config::inst()->get('SSViewer', 'theme') . '/css/editor.css');
63
		return $this->renderWith('PreviewFrame');
64
	}
65
66
	/**
67
	 * Parse markdown into html
68
	 * @return string html
69
	 */
70
	public function parse() {
71
		$parser = new MarkdownParser($this->request['markdown']);
72
73
		return ($this->enable_extra) ? $parser->parseExtra() : $parser->parse(); 
74
	}
75
76
	/**
77
	 * Get buttons described in buttons.yml and wrap them in ViewableData
78
	 * @return ArrayList list of buttons and theyr configurations
79
	 */
80
	public function ToolbarButtons() {
81
		$buttons = new ArrayList();
82
83
		foreach($this->config()->get('buttons') as $button) {
0 ignored issues
show
Bug introduced by
The expression $this->config()->get('buttons') of type array|integer|double|string|boolean 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...
84
			$buttons->push(new ArrayData($button));
85
		}
86
87
		return $buttons;
88
	}
89
90
}
91