Completed
Push — master ( 694826...a7f5ef )
by Ingo
26:11 queued 15:49
created

GoogleSpell.php ➔ mb_substr()   C

Complexity

Conditions 13
Paths 6

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 13
eloc 24
nc 6
nop 4
dl 0
loc 36
rs 5.1234

How to fix   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
 * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
4
 *
5
 * @package MCManager.includes
6
 * @author Moxiecode
7
 * @copyright Copyright � 2004-2007, Moxiecode Systems AB, All rights reserved.
8
 */
9
10
class GoogleSpell extends SpellChecker {
11
	/**
12
	 * Spellchecks an array of words.
13
	 *
14
	 * @param {String} $lang Language code like sv or en.
0 ignored issues
show
Documentation introduced by
The doc-type {String} could not be parsed: Unknown type name "{String}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
15
	 * @param {Array} $words Array of words to spellcheck.
0 ignored issues
show
Documentation introduced by
The doc-type {Array} could not be parsed: Unknown type name "{Array}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
16
	 * @return {Array} Array of misspelled words.
0 ignored issues
show
Documentation introduced by
The doc-type {Array} could not be parsed: Unknown type name "{Array}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
17
	 */
18
	function &checkWords($lang, $words) {
19
		$wordstr = implode(' ', $words);
20
		$matches = $this->_getMatches($lang, $wordstr);
21
		$words = array();
22
23
		for ($i=0; $i<count($matches); $i++)
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
24
			$words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
25
26
		return $words;
27
	}
28
29
	/**
30
	 * Returns suggestions of for a specific word.
31
	 *
32
	 * @param {String} $lang Language code like sv or en.
0 ignored issues
show
Documentation introduced by
The doc-type {String} could not be parsed: Unknown type name "{String}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
33
	 * @param {String} $word Specific word to get suggestions for.
0 ignored issues
show
Documentation introduced by
The doc-type {String} could not be parsed: Unknown type name "{String}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
34
	 * @return {Array} Array of suggestions for the specified word.
0 ignored issues
show
Documentation introduced by
The doc-type {Array} could not be parsed: Unknown type name "{Array}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
35
	 */
36
	function &getSuggestions($lang, $word) {
37
		$sug = array();
38
		$osug = array();
39
		$matches = $this->_getMatches($lang, $word);
40
41
		if (count($matches) > 0)
42
			$sug = explode("\t", utf8_encode($this->_unhtmlentities($matches[0][4])));
43
44
		// Remove empty
45
		foreach ($sug as $item) {
46
			if ($item)
47
				$osug[] = $item;
48
		}
49
50
		return $osug;
51
	}
52
53
	function &_getMatches($lang, $str) {
54
		$server = "www.google.com";
55
		$port = 443;
56
		$path = "/tbproxy/spell?lang=" . $lang . "&hl=en";
57
		$host = "www.google.com";
58
		$url = "https://" . $server;
59
60
		// Setup XML request
61
		$xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';
62
63
		$header  = "POST ".$path." HTTP/1.0 \r\n";
64
		$header .= "MIME-Version: 1.0 \r\n";
65
		$header .= "Content-type: application/PTI26 \r\n";
66
		$header .= "Content-length: ".strlen($xml)." \r\n";
67
		$header .= "Content-transfer-encoding: text \r\n";
68
		$header .= "Request-number: 1 \r\n";
69
		$header .= "Document-type: Request \r\n";
70
		$header .= "Interface-Version: Test 1.4 \r\n";
71
		$header .= "Connection: close \r\n\r\n";
72
		$header .= $xml;
73
74
		// Use curl if it exists
75
		if (function_exists('curl_init')) {
76
			// Use curl
77
			$ch = curl_init();
78
			curl_setopt($ch, CURLOPT_URL,$url);
79
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
80
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
81
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
82
			$xml = curl_exec($ch);
83
			curl_close($ch);
84
		} else {
85
			// Use raw sockets
86
			$fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
87
			if ($fp) {
88
				// Send request
89
				fwrite($fp, $header);
90
91
				// Read response
92
				$xml = "";
93
				while (!feof($fp))
94
					$xml .= fgets($fp, 128);
95
96
				fclose($fp);
97
			} else
98
				echo "Could not open SSL connection to google.";
99
		}
100
101
		// Grab and parse content
102
		$matches = array();
103
		preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
104
105
		return $matches;
106
	}
107
108
	function _unhtmlentities($string) {
109
		$string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
110
		$string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
111
112
		$trans_tbl = get_html_translation_table(HTML_ENTITIES);
113
		$trans_tbl = array_flip($trans_tbl);
114
115
		return strtr($string, $trans_tbl);
116
	}
117
}
118
119
// Patch in multibyte support
120
if (!function_exists('mb_substr')) {
121
	function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
0 ignored issues
show
Unused Code introduced by
The parameter $encoding is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
		$limit = strlen($str);
123
124
		for ($s = 0; $start > 0;--$start) {// found the real start
125
			if ($s >= $limit)
126
				break;
127
128
			if ($str[$s] <= "\x7F")
129
				++$s;
130
			else {
131
				++$s; // skip length
132
133
				while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
134
					++$s;
135
			}
136
		}
137
138
		if ($len == '')
139
			return substr($str, $s);
140
		else
141
			for ($e = $s; $len > 0; --$len) {//found the real end
142
				if ($e >= $limit)
143
					break;
144
145
				if ($str[$e] <= "\x7F")
146
					++$e;
147
				else {
148
					++$e;//skip length
149
150
					while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
151
						++$e;
152
				}
153
			}
154
155
		return substr($str, $s, $e - $s);
156
	}
157
}
158
159
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
160