Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow generated classes target dir to be null #37

Closed
wants to merge 1 commit into from

Conversation

matthiasnoback
Copy link

Because of the string casting, in my code I have to call setGeneratedClassesTargetDir() like this:

class HydrateUsingGeneratedHydrator implements Hydrate
{
    /**
     * @var string|null
     */
    private $cacheDir;

    public function __construct($cacheDir)
    {
        $this->cacheDir = $cacheDir;
    }

    public function hydrate(array $data, $object)
    {
        $configuration = new Configuration(get_class($object));

        if ($this->cacheDir !== null) {
            $configuration->setGeneratedClassesTargetDir($this->cacheDir);
        }
        ...
    }
}

The proposed change allows me to just call $configuration->setGeneratedClassesTargetDir($this->cacheDir); without the extra if clause.

Because of the string casting, in my code I have to call `setGeneratedClassesTargetDir()` like this:

```php
class HydrateUsingGeneratedHydrator implements Hydrate
{
    /**
     * @var string|null
     */
    private $cacheDir;

    public function __construct($cacheDir)
    {
        $this->cacheDir = $cacheDir;
    }

    public function hydrate(array $data, $object)
    {
        $configuration = new Configuration(get_class($object));

        if ($this->cacheDir !== null) {
            $configuration->setGeneratedClassesTargetDir($this->cacheDir);
        }
        ...
}
```

The proposed change allows me to just call `$configuration->setGeneratedClassesTargetDir($this->cacheDir);` without the extra `if` clause.
@@ -164,7 +164,7 @@ public function getGeneratedClassesNamespace()
*/
public function setGeneratedClassesTargetDir($generatedClassesTargetDir)
{
$this->generatedClassesTargetDir = (string) $generatedClassesTargetDir;
$this->generatedClassesTargetDir = $generatedClassesTargetDir;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks internal data integrity of the Configuration object, and may cause exceptions at a later execution point in time.

@Ocramius
Copy link
Owner

Ocramius commented Jul 7, 2015

@matthiasnoback I don't understand the use-case. What are you passing in as a path?

@matthiasnoback
Copy link
Author

I agree that it isn't ideal from the point of view of internal data integrity. But null is a valid option here (since you later turn null into sys_get_temp_dir()). So I'd like to just blindly pass in either null or a string. So maybe it would be better to do something like:

if (!(is_string($generatedClassesTargetDir) || $generatedClassesTargetDir === null)) {
    // throw exception
}

$this->generatedClassesTargetDir = $generatedClassesTargetDir;

@matthiasnoback
Copy link
Author

Maybe I wasn't clear: the problem is that my null value becomes '' because of (string).

@Ocramius
Copy link
Owner

Ocramius commented Jul 7, 2015

@matthiasnoback should I apply stricter validation and throw an exception? I'm fine with that approach.

@matthiasnoback
Copy link
Author

That, or maybe

$this->generatedClassesTargetDir = $generatedClassesTargetDir === null ? null : (string) $generatedClassesTargetDir;

@Ocramius
Copy link
Owner

Ocramius commented Jul 7, 2015

@matthiasnoback I'd rather always expect a string, and use the sys_get_tmp_dir() only as a fallback for when nothing was set explicitly (at all)

@matthiasnoback
Copy link
Author

Okay, no problem. Then I guess we'll leave it as is.

@Ocramius
Copy link
Owner

Ocramius commented Jul 7, 2015

@matthiasnoback the docblock says string, so if you want to add an exception, feel free to do so.

@matthiasnoback
Copy link
Author

Well, in theory the current code would also accept __toString()-able objects, so let's not change that. This was just a really small inconvenience to begin with. Closing this now :) Thanks for your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants