Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Filesystem] Changed dumpFile to allow dumping to streams #16156

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

Merged
merged 5 commits into from
Nov 9, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update coding standard for MockStream
  • Loading branch information
pierredup committed Nov 9, 2015
commit 247266cdef84e6352173f564de06151027550c8c
16 changes: 13 additions & 3 deletions 16 src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,24 @@ public function tempnam($dir, $prefix)

// If no scheme or scheme is "file" create temp file in local filesystem
if (null === $scheme || 'file' === $scheme) {

Copy link
Member

Choose a reason for hiding this comment

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

extra line

$tmpFile = tempnam($hierarchy, $prefix);

// If tempnam failed or no scheme return the filename otherwise prepend the scheme
return false === $tmpFile || null === $scheme ? $tmpFile : $scheme.'://'.$tmpFile;

if (null !== $scheme) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is wrong, cause if schema exisits but tempnam fail you will return: schema://.

This should be:

if (false !== $tmpFile) {
    if (null !== $scheme) {
        return $scheme.'://'.$tmpFile;
    }

    return $tmpFile;
}

return $scheme.'://'.$tmpFile;
}

if (false !== $tmpFile) {
return $tmpFile;
}

throw new IOException('A temporary file could not be created');
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a dot at the end of the error message (that's a convention in Symfony).

}

// Loop until we create a valid temp file or have reached $limit attempts
for ($i = 0; $i < $limit; $i++) {
for ($i = 0; $i < $limit; ++$i) {

Copy link
Member

Choose a reason for hiding this comment

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

extra line

// Create a unique filename
$tmpFile = $dir.'/'.$prefix.uniqid(mt_rand(), true);
Expand Down Expand Up @@ -560,7 +570,7 @@ private function getSchemeAndHierarchy($filename)
{
$components = explode('://', $filename, 2);

return count($components) >= 2 ? array($components[0], $components[1]) : array(null, $components[0]);
return count($components) === 2 ? array($components[0], $components[1]) : array(null, $components[0]);
Copy link
Member

Choose a reason for hiding this comment

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

Can you use a Yoda-condition here? 2 === count($components)

}

Copy link
Member

Choose a reason for hiding this comment

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

extra line here also

}
4 changes: 0 additions & 4 deletions 4 src/Symfony/Component/Filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ $filesystem->rename($origin, $target);

$filesystem->symlink($originDir, $targetDir, $copyOnWindows = false);

$filesystem->tempnam($dir, $prefix);

$filesystem->makePathRelative($endPath, $startPath);

$filesystem->mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array());

$filesystem->isAbsolutePath($file);

$filesystem->dumpFile($file, $content);
```

Resources
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
<?php

/**
/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien@symfony.com>.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This class is based on VariableStream from the PHP Manual, which is licenced
* under Creative Commons Attribution 3.0 Licence copyright (c) the PHP
* Documentation Group
*
* @url http://php.net/manual/en/stream.streamwrapper.example-1.php
* @url http://php.net/license/
* @url http://creativecommons.org/licenses/by/3.0/legalcode
*/

namespace MockStream;
Copy link
Member

Choose a reason for hiding this comment

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

the namespace should be psr-0


/**
* Mock stream class to be used with stream_wrapper_register.
*
* stream_wrapper_register('mock', 'MockStream\MockStream')
* stream_wrapper_register('mock', 'MockStream\MockStream').
*/
class MockStream {
private $str_overloaded;
class MockStream
{
private $strOverloaded;
private $content;
private $position;
private $atime;
Expand All @@ -37,13 +31,16 @@ class MockStream {
* @param string $path Specifies the URL that was passed to the original function
* @param string $mode The mode used to open the file, as detailed for fopen()
* @param int $options Holds additional flags set by the streams API
* @param string $opened_path If the path is opened successfully, and STREAM_USE_PATH is set in options, opened_path should be set to the full path of the file/resource that was actually opened
* @param string $opened_path If the path is opened successfully, and STREAM_USE_PATH is set in options,
* opened_path should be set to the full path of the file/resource that was actually
* opened
*
* @return bool
*/
public function stream_open($path, $mode, $options, &$opened_path) {
public function stream_open($path, $mode, $options, &$opened_path)
{
// Is mbstring.func_overload applied to string functions (bit 2 set)
$this->str_overloaded = (bool) (ini_get('mbstring.func_overload') & (1 << 2));
$this->strOverloaded = (bool) (ini_get('mbstring.func_overload') & (1 << 2));
$this->path = $path;
$this->content = '';
$this->position = 0;
Expand All @@ -60,8 +57,9 @@ public function stream_open($path, $mode, $options, &$opened_path) {
*
* @return string The data
*/
public function stream_read($count) {
$ret = $this->substr($this->varname, $this->position, $count);
public function stream_read($count)
{
$ret = $this->substr($this->content, $this->position, $count);
Copy link
Member

Choose a reason for hiding this comment

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

for what's the mock is used for, almost all implementations should be left empty. It's dead code currently.

$this->position += $this->strlen($ret);
$this->atime = time();

Expand All @@ -75,7 +73,8 @@ public function stream_read($count) {
*
* @return int Number of bytes that were successfully stored, or 0 if none could be stored
*/
public function stream_write($data) {
public function stream_write($data)
{
$left = $this->substr($this->content, 0, $this->position);
$right = $this->substr($this->content, $this->position + $this->strlen($data));
$this->content = $left.$data.$right;
Expand All @@ -91,50 +90,54 @@ public function stream_write($data) {
*
* @return int The current position of the stream
*/
public function stream_tell() {
public function stream_tell()
{
return $this->position;
}

/**
* Tests for end-of-file on a file pointer.
*
* @return bool Return true if the read/write position is at the end of the stream and if no more data is available to be read, or false otherwise
* @return bool Return true if the read/write position is at the end of the stream and if no more data is available
* to be read, or false otherwise
*/
public function stream_eof() {
public function stream_eof()
{
return $this->position >= $this->strlen($this->content);
}

/**
* Seeks to specific location in a stream.
*
* @param string $offset The stream offset to seek to
* @param int $whence Set position based on value
* @param int $offset The stream offset to seek to
* @param int $whence Set position based on value
*
* @return bool Return true if the position was updated, false otherwise
*/
public function stream_seek($offset, $whence) {
public function stream_seek($offset, $whence)
{
switch ($whence) {
case SEEK_SET:
if ($offset < $this->strlen($this->content) && 0 <= $offset) {
$this->position = $offset;
$this->position = $offset;

return true;
return true;
}
break;

case SEEK_CUR:
if (0 <= $offset) {
$this->position += $offset;
$this->position += $offset;

return true;
return true;
}
break;

case SEEK_END:
if (0 <= $this->strlen($this->content) + $offset) {
$this->position = $this->strlen($this->content) + $offset;
$this->position = $this->strlen($this->content) + $offset;

return true;
return true;
}
break;
}
Expand All @@ -151,7 +154,8 @@ public function stream_seek($offset, $whence) {
*
* @return bool Return true on success or fale on failure or if option is not implemented
*/
public function stream_metadata($path, $option, $value) {
public function stream_metadata($path, $option, $value)
{
if (STREAM_META_TOUCH === $option) {
$now = array_key_exists(0, $value) ? $value[0] : time();
$this->atime = array_key_exists(1, $value) ? $value[1] : $now;
Expand All @@ -169,7 +173,8 @@ public function stream_metadata($path, $option, $value) {
*
* @return array Stream stats
*/
public function stream_stat() {
public function stream_stat()
{
return array(
'dev' => 0,
'ino' => 0,
Expand All @@ -195,7 +200,8 @@ public function stream_stat() {
*
* @return array File stats
*/
public function url_stat($path, $flags) {
public function url_stat($path, $flags)
{
return $this->stream_stat();
}

Expand All @@ -206,21 +212,22 @@ public function url_stat($path, $flags) {
*
* @return int The number of bytes in the string on success, and 0 if the string is empty
*/
protected function strlen($string) {
return function_exists('mb_strlen') && $this->str_overloaded ? mb_strlen($string, '8bit') : strlen($string);
protected function strlen($string)
{
return function_exists('mb_strlen') && $this->strOverloaded ? mb_strlen($string, '8bit') : strlen($string);
Copy link
Member

Choose a reason for hiding this comment

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

we don't care about mbstring overloading in Symfony, please use strlen directly

}

/**
* Returns the portion of string specified by the start and length parameters even when substr is overloaded to mb_substr.
*
* @param string $string The input string which must be one character or longer
* @param start $int Starting position in bytes
* @param length $int Length in bytes which if omitted or NULL is passed, extracts all bytes to the end of the string
* @param int $start Starting position in bytes
* @param int $length Length in bytes which if omitted or NULL is passed, extracts all bytes to the end of the string
*
* @return string
*/
protected function substr($string, $start, $length = null) {
return function_exists('mb_substr') && $this->str_overloaded ? mb_substr($string, $start, $length, '8bit') : substr($string, $start, $length);
protected function substr($string, $start, $length = null)
{
return function_exists('mb_substr') && $this->strOverloaded ? mb_substr($string, $start, $length, '8bit') : substr($string, $start, $length);
Copy link
Member

Choose a reason for hiding this comment

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

see above

}

}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.