RelationData::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 3 Features 2
Metric Value
cc 6
eloc 19
c 6
b 3
f 2
nc 9
nop 4
dl 0
loc 38
rs 8.439
1
<?php
2
namespace samson\activerecord;
3
4
/**
5
 * Class for relation table/class data storage on joining tables
6
 * 
7
 * @author Vitaly Iegorov <[email protected]>
8
 */
9
class RelationData 
10
{
11
	/** Base class in relation */
12
	public $base;
13
	
14
	/** Relative class in relation */
15
	public $relation;
16
	
17
	/** Real table name name or alias table name in relation */
18
	public $table;
19
20
21
    /** Full table name as stored in database(with prefix) */
22
    public $realTableName;
23
24
    /** Short table name as stored everywhere in code (without prefix) */
25
    public $virtualTableName;
26
27
    /** Alias table name */
28
    public $aliasTableName;
29
30
    /** Class name for creating table instances */
31
    public $className;
32
33
    /** Flag for ignoring this class, and do not create instances of this class */
34
    public $ignore = false;
35
	
36
	/**
37
	 * Constructor	  
38
	 * @param string  $base_class	    Base class in relation
39
	 * @param string  $table_name	    Name/alias of table in relation
0 ignored issues
show
Documentation introduced by
There is no parameter named $table_name. Did you maybe mean $table_name_simple?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
40
	 * @param string  $relation_class   Classname that has to be created on joining
41
     * @param boolean $ignore           Flag for not creating object instances for this class
42
	 */
43
	public function __construct( $base_class, $table_name_simple, $relation_class = null, $ignore = false )
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between opening bracket and argument "$base_class"; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces between argument "$ignore" and closing bracket; 1 found
Loading history...
44
	{				
45
		// If table name passed without namespace consider it as activerecord namespace
46
		$table_name = \samson\core\AutoLoader::className( $table_name_simple, 'samson\activerecord');
47
48
		// If relation class not specified
49
		if (!isset($relation_class)) {
50
			// if there is no class exists for table name specified
51
			if (!class_exists($table_name, false)) {
52
				// PHP < 5.3 get relation aliases
53
				eval('$_relation_alias = '.$base_class.'::$_relation_alias;');
54
					
55
				// Try to find classname in relation aliases
56
                if( isset($_relation_alias[ $table_name_simple ])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
57
                    $relation_class = \samson\core\AutoLoader::className($_relation_alias[ $table_name_simple ], __NAMESPACE__);
58
                } else if (isset($_relation_alias[ $table_name ])) {
0 ignored issues
show
Bug introduced by
The variable $_relation_alias seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
59
                    $relation_class = \samson\core\AutoLoader::className($_relation_alias[ $table_name ], __NAMESPACE__);
60
                } else { // use thi table name as class
61
                    $relation_class = $table_name;
62
                }
63
			}
64
			// Relation class name equals to table name
65
			else $relation_class = $table_name;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
66
67
			// Try to find closest parent class to dbRecord class
68
			$parent_class = get_parent_class( $relation_class );
69
			if( $parent_class != \samson\core\AutoLoader::className( 'dbRecord', 'samson\activerecord')) $table_name = \samson\core\AutoLoader::getOnlyClass($parent_class);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
70
		}			
71
			
72
		// Set defined class fields
73
		$this->base = $base_class;
74
		$this->relation = $relation_class;
75
		$this->table = \samson\core\AutoLoader::getOnlyClass( $table_name );
76
        $this->ignore = $ignore;
77
	
78
		// TODO: fix this problem
79
		$this->table = str_replace('samson_activerecord_', '', $this->table);	
80
	}
81
}