review_2011_11_12_alex.md 5.32 KB
Newer Older
Alexander Makarov committed
1 2 3 4 5 6 7 8 9
Alex's Code Review, 2011.11.12
==============================

Overall hierarchy
------------------

Generally is OK. Like that `Object` and `Component` are now separated.
I've generated 2 diagrams under `docs/` to see it better as a whole.

Qiang Xue committed
10 11 12 13 14 15
> The purpose of separating `Object` from `Component` is to make `Object`
> a super-light base class that supports properties defined by getter/setters.
> Note that `Component` is a bit of heavy because it uses two extra member
> variables to support events and behaviors.


Alexander Makarov committed
16 17 18 19 20 21 22 23
Object
------

### property feature

Is it OK that `canGetProperty` and `canSetProperty` will return `false` for real
class members?

Qiang Xue committed
24 25
> Added $checkVar parameter

Alexander Makarov committed
26 27 28 29 30 31 32 33 34
### callbacks and expressions

We're using 5.3. What's the reason to support `eval()` in `evaluateExpression` if
we have anonymous functions? Is that for storing code as string inside of DB (RBAC)?

If we're going to get rid of `eval()`, cosider remaning method to something about callback.
If not then we definitely need to use anonymous functions in API docs and the guide
where possible.

Qiang Xue committed
35 36 37
> The purpose of evaluateExpression() is to provide a way of evaluating a PHP expression
> in the context of an object. Will remove it before release if we find no use of it.

mdomba (mdlap) committed
38 39 40 41 42
>> mdomba:
>> As eval() is controversial, and anonymous functions can replace all Yii 1 usage of eval()
>> how about removing it from the beginning and add it only if we find it necessary.
>> This way we would not be tempted to stick with eval() and will be forced to first try to find alternatives

Alexander Makarov committed
43 44 45 46 47 48 49 50
### Object::create()

#### `__construct` issue

Often a class doesn't have `__construct` implementation and `stdClass` doesn't have
default one either but Object::create() always expects constructor to be
defined. See `ObjectTest`. Either `method_exists` call or `Object::__construct` needed.

Qiang Xue committed
51 52
> Added Object::__construct.

Alexander Makarov committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#### How to support object factory like we do with CWidgetFactory?

~~~
class ObjectConfig
{
	public function configure($class)
	{
		$config = $this->load($class);
		// apply config to $class
	}

	private function load($class)
	{
		// get class properties from a config file
		// in this method we need to walk all the
		// inheritance hierarchy down to Object itself
		return array(
			'property' => 'value',
			// …
		);
	}
}
~~~

Then we need to add `__construct` to `Object` (or implement `Initalbe`):

~~~
class Object
{
	public function __construct()
	{
		$conf = new ObjectConfig();
		$conf->configure($this);
	}
}
~~~

This way we'll be able to set defaults for any object.

Qiang Xue committed
92 93 94 95
> The key issue here is about how to process the config file. Clearly, we cannot
> do this for every type of component because it would mean an extra file access
> for every component type

Alexander Makarov committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#### Do we need to support lazy class injection?

Currently there's no way to lazy-inject class into another class property via
config. Do we need it? If yes then we can probably extend component config to support
the following:

~~~
class Foo extends Object
{
	public $prop;
}

class Bar extends Object
{
	public $prop;
}

$config = array(
	'prop' => array(
		'class' => 'Bar',
		'prop' => 'Hello!',
	),
);

$foo = Foo::create($config);
echo $foo->bar->prop;
// will output Hello!
~~~

Should it support infinite nesting level?

Qiang Xue committed
127 128 129 130 131
> I don't think we need this. Foo::$prop cannot be an object unless it needs it to be.
> In that case, it can be defined with a setter in which it can handle the object creation
> based on a configuration array. This is a bit inconvenient, but I think such usage is
> not very common.

Alexander Makarov committed
132 133 134 135 136
### Why `Event` is `Object`?

There's no need to extend from `Object`. Is there a plan to use `Object` features
later?

Qiang Xue committed
137
> To use properties defined via getter/setter.
Alexander Makarov committed
138 139 140 141 142 143 144 145 146 147 148


Behaviors
---------

Overall I wasn't able to use behaviors. See `BehaviorTest`.

### Should behaviors be able to define events for owner components?

Why not? Should be a very good feature in order to make behaviors customizable.

Qiang Xue committed
149 150 151
> It's a bit hard to implement it efficiently. I tend not to support it for now
> unless enough people are requesting for it.

Alexander Makarov committed
152 153 154 155
### Multiple behaviors can be attached to the same component

What if we'll have multiple methods / properties / events with the same name?

Qiang Xue committed
156 157
> The first one takes precedence. This is the same as we do in 1.1.

Alexander Makarov committed
158 159 160 161 162
### How to use Behavior::attach?

Looks like it is used by `Component::attachBehavior` but can't be used without it.
Why it's public then? Can we move it to `Component?`

Qiang Xue committed
163 164 165
> It's public because it is called by Component. It is in Behavior such that
> it can be overridden by behavior classes to customize the attach process.

Alexander Makarov committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
Events
------

Class itself looks OK. Component part is OK as well but I've not tested
it carefully. Overall it seems concept is the same as in Yii1.

### Event declaration: the on-method is mostly repetitive for every event. Should we choose a different way of declaring events?

Maybe. People complained previously about too many code for event declaration.

### Should we implement some additional event mechanism, such as global events?

Why use two different implementations in a single application?

Exceptions
----------

- Should we convert all errors, warnings and notices to exceptions?

Qiang Xue committed
185 186 187 188
> I think not. We used to do this in early versions of 1.0. We found sometimes
> very mysterious things would happen which makes error fixing harder rather than
> easier.

Alexander Makarov committed
189 190 191 192
Coding style
------------

See `docs/code_style.md`.