Completed
Pull Request — master (#287)
by Nic
07:49
created

FoxyStripePurchaseForm   B

Complexity

Total Complexity 35

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 15
dl 0
loc 236
c 0
b 0
f 0
rs 8.25

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setSiteConfig() 0 9 3
A getSiteConfig() 0 7 2
A setProduct() 0 8 2
A getProduct() 0 4 1
D __construct() 0 36 9
B getProductFields() 0 58 8
B getProductActions() 0 17 5
B getProductOptionSet() 0 41 5
1
<?php
2
3
/**
4
 * Class FoxyStripePurchaseForm
5
 *
6
 * @property SiteConfig $site_config
7
 * @property FoxyStripeProduct $product
8
 *
9
 */
10
class FoxyStripePurchaseForm extends Form
11
{
12
13
    /**
14
     * @var
15
     */
16
    protected $site_config;
17
    /**
18
     * @var
19
     */
20
    private $product;
21
22
    /**
23
     * @param $siteConfig
24
     * @return $this
25
     */
26
    public function setSiteConfig($siteConfig)
27
    {
28
        $siteConfig = $siteConfig === null ? SiteConfig::current_site_config() : $siteConfig;
29
        if ($siteConfig instanceof SiteConfig) {
30
            $this->site_config = $siteConfig;
31
            return $this;
32
        }
33
        throw new InvalidArgumentException('$siteConfig needs to be an instance of SiteConfig.');
34
    }
35
36
    /**
37
     * @return SiteConfig
38
     */
39
    public function getSiteConfig()
40
    {
41
        if (!$this->site_config) {
42
            $this->setSiteConfig(SiteConfig::current_site_config());
43
        }
44
        return $this->site_config;
45
    }
46
47
    /**
48
     * @param $product
49
     * @return $this
50
     */
51
    public function setProduct($product)
52
    {
53
        if ($product instanceof FoxyStripeProduct) {
54
            $this->product = $product;
55
            return $this;
56
        }
57
        throw new InvalidArgumentException('$product needs to be an instance of FoxyStripeProduct.');
58
    }
59
60
    /**
61
     * @return FoxyStripeProduct
62
     */
63
    public function getProduct()
64
    {
65
        return $this->product;
66
    }
67
68
    /**
69
     * FoxyStripePurchaseForm constructor.
70
     * @param Controller $controller
71
     * @param string $name
72
     * @param FieldList|null $fields
73
     * @param FieldList|null $actions
74
     * @param null $validator
75
     * @param null $product
76
     * @param null $siteConfig
77
     */
78
    public function __construct(
79
        $controller,
80
        $name,
81
        FieldList $fields = null,
82
        FieldList $actions = null,
83
        $validator = null,
84
        $product = null,
85
        $siteConfig = null
86
    )
87
    {
88
        $this->setProduct($product);
89
        $this->setSiteConfig($siteConfig);
90
91
        $fields = ($fields != null && $fields->exists()) ? $this->getProductFields($fields) : $this->getProductFields(FieldList::create());
92
93
        $actions = ($actions != null && $actions->exists()) ? $this->getProductActions($actions) : $this->getProductActions(FieldList::create());
94
        $validator = (!empty($validator) || $validator != null) ? $validator : RequiredFields::create();
95
96
        Requirements::javascript("framework/thirdparty/jquery/jquery.js");
97
        if ($product->Available && $product->ProductOptions()->exists()) {
98
            Requirements::javascript("foxystripe/javascript/outOfStock.min.js");
99
            Requirements::javascript("foxystripe/javascript/ProductOptions.min.js");
100
        }
101
102
        Requirements::customScript(<<<JS
103
		var productID = {$product->ID};
104
JS
105
        );
106
107
        parent::__construct($controller, $name, $fields, $actions, $validator);
108
109
        //have to call after parent::__construct()
110
        $this->setAttribute('action', FoxyCart::FormActionURL());
111
        $this->disableSecurityToken();
112
113
    }
114
115
    /**
116
     * @param FieldList $fields
117
     * @return FieldList
118
     */
119
    protected function getProductFields(FieldList $fields)
120
    {
121
        $hiddenTitle = ($this->product->ReceiptTitle) ? htmlspecialchars($this->product->ReceiptTitle) : htmlspecialchars($this->product->Title);
0 ignored issues
show
Documentation introduced by
The property ReceiptTitle does not exist on object<FoxyStripeProduct>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
122
        $code = $this->product->Code;
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<FoxyStripeProduct>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
123
124
        if ($this->product->Available) {
0 ignored issues
show
Documentation introduced by
The property Available does not exist on object<FoxyStripeProduct>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
125
            $fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'name',
126
                $hiddenTitle))->setValue($hiddenTitle));
127
            $fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'category',
128
                $this->product->Category()->Code))->setValue($this->product->Category()->Code));
0 ignored issues
show
Documentation Bug introduced by
The method Category does not exist on object<FoxyStripeProduct>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
129
            $fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'code',
130
                $this->product->Code))->setValue($this->product->Code));
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<FoxyStripeProduct>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
131
            $fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'product_id',
132
                $this->product->ID))->setValue($this->product->ID));
133
            $fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'price',
134
                $this->product->Price))->setValue($this->product->Price));//can't override id
0 ignored issues
show
Documentation introduced by
The property Price does not exist on object<FoxyStripeProduct>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
135
            $fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'weight',
136
                $this->product->Weight))->setValue($this->product->Weight));
0 ignored issues
show
Documentation introduced by
The property Weight does not exist on object<FoxyStripeProduct>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
137
            if ($this->DiscountTitle && $this->ProductDiscountTiers()->exists()) {
0 ignored issues
show
Documentation introduced by
The property DiscountTitle does not exist on object<FoxyStripePurchaseForm>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation Bug introduced by
The method ProductDiscountTiers does not exist on object<FoxyStripePurchaseForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
138
                $fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'discount_quantity_percentage',
139
                    $this->product->getDiscountFieldValue()))->setValue($this->product->getDiscountFieldValue()));
140
            }
141
142
143
            if ($this->product->PreviewImage()->exists()) {
0 ignored issues
show
Documentation Bug introduced by
The method PreviewImage does not exist on object<FoxyStripeProduct>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
144
                $fields->push(
145
                    HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'image',
146
                        $this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL))
0 ignored issues
show
Documentation Bug introduced by
The method PreviewImage does not exist on object<FoxyStripeProduct>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
147
                        ->setValue($this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL)
0 ignored issues
show
Documentation Bug introduced by
The method PreviewImage does not exist on object<FoxyStripeProduct>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
148
                );
149
            }
150
151
            $optionsSet = $this->getProductOptionSet();
152
            $fields->push($optionsSet);
153
154
            $quantityMax = ($this->site_config->MaxQuantity) ? $this->site_config->MaxQuantity : 10;
155
            $count = 1;
156
            $quantity = array();
157
            while ($count <= $quantityMax) {
158
                $countVal = FoxyStripeProduct::getGeneratedValue($this->product->Code, 'quantity', $count, 'value');
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<FoxyStripeProduct>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
159
                $quantity[$countVal] = $count;
160
                $count++;
161
            }
162
163
            $fields->push(DropdownField::create('quantity', 'Quantity', $quantity));
164
165
            $fields->push(HeaderField::create('submitPrice', '$' . $this->product->Price, 4));
0 ignored issues
show
Documentation introduced by
The property Price does not exist on object<FoxyStripeProduct>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
166
167
168
            $this->extend('updatePurchaseFormFields', $fields);
169
        } else {
170
            $fields->push(HeaderField::create('submitPrice', 'Currently Out of Stock'), 4);
0 ignored issues
show
Unused Code introduced by
The call to FieldList::push() has too many arguments starting with 4.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
171
        }
172
173
        $this->extend('updateFoxyStripePurchaseFormFields', $fields);
174
175
        return $fields;
176
    }
177
178
    /**
179
     * @param FieldList $actions
180
     * @return FieldList
181
     */
182
    protected function getProductActions(FieldList $actions)
183
    {
184
185
        $actions->push($submit = FormAction::create(
186
            '',
187
            _t('ProductForm.AddToCart', 'Add to Cart')
188
        ));
189
        $submit->setAttribute('name',
190
            FoxyStripeProduct::getGeneratedValue($this->product->Code, 'Submit', _t('ProductForm.AddToCart', 'Add to Cart')));
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<FoxyStripeProduct>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
191
        if (!$this->site_config->StoreName || $this->site_config->StoreName == '' || !isset($this->site_config->StoreName) || !$this->product->Available) {
0 ignored issues
show
Documentation introduced by
The property Available does not exist on object<FoxyStripeProduct>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
192
            $submit->setAttribute('Disabled', true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193
        }
194
195
        $this->extend('updateFoxyStripePurchaseFormActions', $fields);
196
197
        return $actions;
198
    }
199
200
    /**
201
     * @return CompositeField
202
     */
203
    protected function getProductOptionSet()
204
    {
205
206
        $assignAvailable = function ($self) {
207
            $this->extend('updateFoxyStripePurchaseForm', $form);
208
            $self->Available = ($self->getAvailability()) ? true : false;
209
        };
210
211
        $options = $this->product->ProductOptions();
0 ignored issues
show
Documentation Bug introduced by
The method ProductOptions does not exist on object<FoxyStripeProduct>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
212
        $groupedOptions = new GroupedList($options);
213
        $groupedBy = $groupedOptions->groupBy('ProductOptionGroupID');
214
215
        $optionsSet = CompositeField::create();
216
217
        foreach ($groupedBy as $id => $set) {
218
            $group = OptionGroup::get()->byID($id);
219
            $title = $group->Title;
220
            $name = preg_replace('/\s/', '_', $title);
221
            $set->each($assignAvailable);
222
            $disabled = array();
223
            $fullOptions = array();
224
            foreach ($set as $item) {
225
                $fullOptions[FoxyStripeProduct::getGeneratedValue($this->product->Code, $group->Title,
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<FoxyStripeProduct>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
226
                    $item->getGeneratedValue(),
227
                    'value')] = $item->getGeneratedTitle();
228
                if (!$item->Availability) {
229
                    array_push($disabled,
230
                        FoxyStripeProduct::getGeneratedValue($this->product->Code, $group->Title, $item->getGeneratedValue(),
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<FoxyStripeProduct>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
231
                            'value'));
232
                }
233
            }
234
            $optionsSet->push(
235
                $dropdown = DropdownField::create($name, $title, $fullOptions)->setTitle($title)
236
            );
237
            $dropdown->setDisabledItems($disabled);
238
        }
239
240
        $optionsSet->addExtraClass('foxycartOptionsContainer');
241
242
        return $optionsSet;
243
    }
244
245
}