Skip to content

Commit af180d7

Browse files
committed
Add real visibility, adjust tests
1 parent e6a43e6 commit af180d7

File tree

3 files changed

+145
-125
lines changed

3 files changed

+145
-125
lines changed

Text/LanguageDetect.php

Lines changed: 44 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -69,95 +69,85 @@ class Text_LanguageDetect
6969
* If this value starts with a slash (/) or a dot (.) the value of
7070
* $this->_data_dir will be ignored
7171
*
72-
* @var string
73-
* @access private
72+
* @var string
7473
*/
75-
var $_db_filename = 'lang.dat';
74+
protected $_db_filename = 'lang.dat';
7675

7776
/**
7877
* The filename that stores the unicode block definitions
7978
*
8079
* If this value starts with a slash (/) or a dot (.) the value of
8180
* $this->_data_dir will be ignored
8281
*
83-
* @var string
84-
* @access private
82+
* @var string
8583
*/
86-
var $_unicode_db_filename = 'unicode_blocks.dat';
84+
protected $_unicode_db_filename = 'unicode_blocks.dat';
8785

8886
/**
8987
* The data directory
9088
*
9189
* Should be set by PEAR installer
9290
*
93-
* @var string
94-
* @access private
91+
* @var string
9592
*/
96-
var $_data_dir = '@data_dir@';
93+
protected $_data_dir = '@data_dir@';
9794

9895
/**
9996
* The trigram data for comparison
10097
*
10198
* Will be loaded on start from $this->_db_filename
10299
*
103-
* @var array
104-
* @access private
100+
* @var array
105101
*/
106-
var $_lang_db = array();
102+
protected $_lang_db = array();
107103

108104
/**
109105
* Stores the map of the trigram data to unicode characters
110106
*
111-
* @access private
112-
* @var array
107+
* @var array
113108
*/
114-
var $_unicode_map;
109+
protected $_unicode_map;
115110

116111
/**
117112
* The size of the trigram data arrays
118113
*
119-
* @var int
120-
* @access private
114+
* @var int
121115
*/
122-
var $_threshold = 300;
116+
protected $_threshold = 300;
123117

124118
/**
125119
* The maximum possible score.
126120
*
127121
* Needed for score normalization. Different depending on the
128122
* perl compatibility setting
129123
*
130-
* @access private
131-
* @var int
132-
* @see setPerlCompatible()
124+
* @var int
125+
* @see setPerlCompatible()
133126
*/
134-
var $_max_score = 0;
127+
protected $_max_score = 0;
135128

136129
/**
137130
* Whether or not to simulate perl's Language::Guess exactly
138131
*
139-
* @access private
140-
* @var bool
141-
* @see setPerlCompatible()
132+
* @var bool
133+
* @see setPerlCompatible()
142134
*/
143-
var $_perl_compatible = false;
135+
protected $_perl_compatible = false;
144136

145137
/**
146138
* Whether to use the unicode block detection to speed up processing
147139
*
148-
* @access private
149-
* @var bool
140+
* @var bool
150141
*/
151-
var $_use_unicode_narrowing = true;
142+
protected $_use_unicode_narrowing = true;
152143

153144
/**
154145
* Stores the result of the clustering operation
155146
*
156-
* @access private
157-
* @var array
158-
* @see clusterLanguages()
147+
* @var array
148+
* @see clusterLanguages()
159149
*/
160-
var $_clusters;
150+
protected $_clusters;
161151

162152
/**
163153
* Which type of "language names" are accepted and returned:
@@ -166,15 +156,15 @@ class Text_LanguageDetect
166156
* 2 - 2-letter ISO 639-1 code ("en")
167157
* 3 - 3-letter ISO 639-2 code ("eng")
168158
*/
169-
var $_name_mode = 0;
159+
protected $_name_mode = 0;
170160

171161
/**
172162
* Constructor
173163
*
174164
* Will attempt to load the language database. If it fails, you will get
175165
* an exception.
176166
*/
177-
function __construct()
167+
public function __construct()
178168
{
179169
$data = $this->_readdb($this->_db_filename);
180170
$this->_checkTrigram($data['trigram']);
@@ -196,9 +186,8 @@ function __construct()
196186
* @param string $fname File name to load
197187
*
198188
* @return string expected path to the language model database
199-
* @access private
200189
*/
201-
function _get_data_loc($fname)
190+
protected function _get_data_loc($fname)
202191
{
203192
if ($fname{0} == '/' || $fname{0} == '.') {
204193
// if filename starts with a slash, assume it's an absolute pathname
@@ -225,9 +214,8 @@ function _get_data_loc($fname)
225214
*
226215
* @return array the language model data
227216
* @throws Text_LanguageDetect_Exception
228-
* @access private
229217
*/
230-
function _readdb($fname)
218+
protected function _readdb($fname)
231219
{
232220
// finds the correct data dir
233221
$fname = $this->_get_data_loc($fname);
@@ -255,9 +243,8 @@ function _readdb($fname)
255243
* @param array $trigram Trigram data from database
256244
*
257245
* @return void
258-
* @access private
259246
*/
260-
function _checkTrigram($trigram)
247+
protected function _checkTrigram($trigram)
261248
{
262249
if (!is_array($trigram)) {
263250
if (ini_get('magic_quotes_runtime')) {
@@ -349,11 +336,10 @@ public function omitLanguages($omit_list, $include_only = false)
349336
/**
350337
* Returns the number of languages that this object can detect
351338
*
352-
* @access public
353339
* @return int the number of languages
354340
* @throws Text_LanguageDetect_Exception
355341
*/
356-
function getLanguageCount()
342+
public function getLanguageCount()
357343
{
358344
return count($this->_lang_db);
359345
}
@@ -391,11 +377,10 @@ public function languageExists($lang)
391377
/**
392378
* Returns the list of detectable languages
393379
*
394-
* @access public
395380
* @return array the names of the languages known to this object<<<<<<<
396381
* @throws Text_LanguageDetect_Exception
397382
*/
398-
function getLanguages()
383+
public function getLanguages()
399384
{
400385
return $this->_convertToNameMode(
401386
array_keys($this->_lang_db)
@@ -433,7 +418,7 @@ public function setPerlCompatible($setting = true)
433418
*
434419
* @return void
435420
*/
436-
function setNameMode($name_mode)
421+
public function setNameMode($name_mode)
437422
{
438423
$this->_name_mode = $name_mode;
439424
}
@@ -463,10 +448,9 @@ public function useUnicodeBlocks($setting = true)
463448
* @param string $text text to convert
464449
*
465450
* @return array array of trigram frequencies
466-
* @access private
467451
* @deprecated Superceded by the Text_LanguageDetect_Parser class
468452
*/
469-
function _trigram($text)
453+
protected function _trigram($text)
470454
{
471455
$s = new Text_LanguageDetect_Parser($text);
472456
$s->prepareTrigram();
@@ -484,9 +468,8 @@ function _trigram($text)
484468
* @param array $arr array of trigram
485469
*
486470
* @return array ranks of trigrams
487-
* @access protected
488471
*/
489-
function _arr_rank($arr)
472+
protected function _arr_rank($arr)
490473
{
491474

492475
// sorts alphabetically first as a standard way of breaking rank ties
@@ -517,9 +500,8 @@ function _arr_rank($arr)
517500
* @param array $arr the array to sort
518501
*
519502
* @return void
520-
* @access private
521503
*/
522-
function _bub_sort(&$arr)
504+
protected function _bub_sort(&$arr)
523505
{
524506
// should do the same as this perl statement:
525507
// sort { $trigrams{$b} == $trigrams{$a}
@@ -557,9 +539,8 @@ function _bub_sort(&$arr)
557539
*
558540
* @return int 1 if $a is greater, -1 if not
559541
* @see _bub_sort()
560-
* @access private
561542
*/
562-
function _sort_func($a, $b)
543+
protected function _sort_func($a, $b)
563544
{
564545
// each is actually a key/value pair, so that it can compare using both
565546
list($a_key, $a_value) = $a;
@@ -597,9 +578,8 @@ function _sort_func($a, $b)
597578
*
598579
* @return int the sum of the differences between the ranks of
599580
* the two trigram sets
600-
* @access private
601581
*/
602-
function _distance($arr1, $arr2)
582+
protected function _distance($arr1, $arr2)
603583
{
604584
$sumdist = 0;
605585

@@ -630,9 +610,8 @@ function _distance($arr1, $arr2)
630610
*
631611
* @return float the normalized score
632612
* @see _distance()
633-
* @access private
634613
*/
635-
function _normalize_score($score, $base_count = null)
614+
protected function _normalize_score($score, $base_count = null)
636615
{
637616
if ($base_count === null) {
638617
$base_count = $this->_threshold;
@@ -971,9 +950,8 @@ public function unicodeBlockName($unicode)
971950
*
972951
* @return mixed Block name, -1 if it failed
973952
* @see unicodeBlockName()
974-
* @access protected
975953
*/
976-
function _unicode_block_name($unicode, $blocks, $block_count = -1)
954+
protected function _unicode_block_name($unicode, $blocks, $block_count = -1)
977955
{
978956
// for a reference, see
979957
// http://www.unicode.org/Public/UNIDATA/Blocks.txt
@@ -1024,9 +1002,8 @@ function _unicode_block_name($unicode, $blocks, $block_count = -1)
10241002
*
10251003
* @return array the database of unicode block definitions
10261004
* @throws Text_LanguageDetect_Exception
1027-
* @access protected
10281005
*/
1029-
function _read_unicode_block_db()
1006+
protected function _read_unicode_block_db()
10301007
{
10311008
// since the unicode definitions are always going to be the same,
10321009
// might as well share the memory for the db with all other instances
@@ -1145,14 +1122,13 @@ public function languageSimilarity($lang1 = null, $lang2 = null)
11451122
* Uses a nearest neighbor technique to generate the maximum possible
11461123
* number of dendograms from the similarity data.
11471124
*
1148-
* @access public
11491125
* @return array language cluster data
11501126
* @throws Text_LanguageDetect_Exception
11511127
* @see languageSimilarity()
11521128
* @deprecated this function will eventually be removed and placed into
11531129
* the model generation class
11541130
*/
1155-
function clusterLanguages()
1131+
public function clusterLanguages()
11561132
{
11571133
// todo: set the maximum number of clusters
11581134
// return cached result, if any
@@ -1485,10 +1461,9 @@ public static function utf8strlen($str)
14851461
* @param string $char a utf8 (possibly multi-byte) char
14861462
*
14871463
* @return int unicode value
1488-
* @access protected
14891464
* @link http://en.wikipedia.org/wiki/UTF-8
14901465
*/
1491-
function _utf8char2unicode($char)
1466+
protected function _utf8char2unicode($char)
14921467
{
14931468
// strlen() here will actually get the binary length of a single char
14941469
switch (strlen($char)) {
@@ -1536,9 +1511,8 @@ function _utf8char2unicode($char)
15361511
* @param bool $special_convert whether to do special conversions
15371512
*
15381513
* @return char the next (possibly multi-byte) char from $counter
1539-
* @access private
15401514
*/
1541-
static function _next_char($str, &$counter, $special_convert = false)
1515+
protected static function _next_char($str, &$counter, $special_convert = false)
15421516
{
15431517
$char = $str{$counter++};
15441518
$ord = ord($char);
@@ -1630,7 +1604,7 @@ static function _next_char($str, &$counter, $special_convert = false)
16301604
*
16311605
* @return string|array Language name
16321606
*/
1633-
function _convertFromNameMode($lang, $convertKey = false)
1607+
protected function _convertFromNameMode($lang, $convertKey = false)
16341608
{
16351609
if ($this->_name_mode == 0) {
16361610
return $lang;
@@ -1670,7 +1644,7 @@ function _convertFromNameMode($lang, $convertKey = false)
16701644
*
16711645
* @return string|array Language name
16721646
*/
1673-
function _convertToNameMode($lang, $convertKey = false)
1647+
protected function _convertToNameMode($lang, $convertKey = false)
16741648
{
16751649
if ($this->_name_mode == 0) {
16761650
return $lang;

tests/PrivProxy.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Helper that enables access to private and protected methods and properties.
4+
*/
5+
class PrivProxy
6+
{
7+
private $obj;
8+
9+
public function __construct($obj)
10+
{
11+
$this->obj = $obj;
12+
}
13+
14+
public function __call($method, $arguments)
15+
{
16+
$rm = new ReflectionMethod($this->obj, $method);
17+
$rm->setAccessible(true);
18+
return $rm->invokeArgs($this->obj, $arguments);
19+
}
20+
21+
public static function __callStatic($method, $arguments)
22+
{
23+
$rm = new ReflectionMethod($this->obj, $method);
24+
$rm->setAccessible(true);
25+
return $rm->invokeArgs($this->obj, $arguments);
26+
}
27+
28+
public function __set($var, $value)
29+
{
30+
$rp = new ReflectionProperty($this->obj, $var);
31+
$rp->setAccessible(true);
32+
$rp->setValue($this->obj, $value);
33+
}
34+
35+
public function __get($var)
36+
{
37+
$rp = new ReflectionProperty($this->obj, $var);
38+
$rp->setAccessible(true);
39+
return $rp->getValue($this->obj);
40+
}
41+
}
42+
?>

0 commit comments

Comments
 (0)