Skip to content

Commit

Permalink
added files
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 13, 2017
1 parent 394934c commit 6ea4df2
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.gitattributes export-ignore
.gitignore export-ignore
.github export-ignore
.travis.yml export-ignore
tests/ export-ignore
*.sh eol=lf
44 changes: 44 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
language: php
php:
- 5.6
- 7.0
- 7.1
- 7.2

before_install:
# turn off XDebug
- phpenv config-rm xdebug.ini || return 0

install:
- travis_retry composer install --no-progress --prefer-dist

script:
- vendor/bin/tester tests -s

after_failure:
# Print *.actual content
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done

jobs:
include:
- stage: Code Standard Checker
php: 7.1
install:
# Install Nette Code Checker
- travis_retry composer create-project nette/code-checker temp/code-checker ~2 --no-progress
# Install Nette Coding Standard
- travis_retry composer create-project nette/coding-standard temp/coding-standard --no-progress

script:
- php temp/code-checker/src/code-checker.php
- php temp/coding-standard/ecs check src tests --config tests/coding-standard.neon;


sudo: false

cache:
directories:
- $HOME/.composer/cache

notifications:
email: false
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "dg/bypass-finals",
"description": "Removes final keyword from source code on-the-fly and allows mocking of final methods and classes",
"keywords": ["testing", "unit", "phpunit", "mocking", "finals"],
"license": ["BSD-3-Clause", "GPL-2.0", "GPL-3.0"],
"authors": [
{
"name": "David Grudl",
"homepage": "https://davidgrudl.com"
}
],
"require": {
"php": ">=5.6"
},
"require-dev": {
"nette/tester": "^2.0"
},
"autoload": {
"classmap": ["src/"]
}
}
49 changes: 49 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Licenses
========

Good news! You may use Nette Tester under the terms of either
the New BSD License or the GNU General Public License (GPL) version 2 or 3.



New BSD License
---------------

Copyright (c) 2004, 2013 David Grudl (https://davidgrudl.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of "Nette Tester" nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright owner or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.



GNU General Public License
--------------------------

GPL licenses are very very long, so instead of including them here we offer
you URLs with full text:

- [GPL version 2](http://www.gnu.org/licenses/gpl-2.0.html)
- [GPL version 3](http://www.gnu.org/licenses/gpl-3.0.html)
39 changes: 39 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Bypass Finals
=============

[![Downloads this Month](https://img.shields.io/packagist/dm/dg/bypass-finals.svg)](https://packagist.org/packages/dg/bypass-finals)
[![Build Status](https://travis-ci.org/dg/bypass-finals.svg?branch=master)](https://travis-ci.org/dg/bypass-finals)
[![Latest Stable Version](https://poser.pugx.org/dg/bypass-finals/v/stable)](https://github.com/dg/bypass-finals/releases)
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/dg/bypass-finals/blob/master/license.md)


Introduction
------------

Removes final keywords from source code on-the-fly and allows mocking of final methods and classes.
It can be used together with any test tool such as PHPUnit or Mockery.


Installation
------------

The recommended way to install is through Composer:

```
composer require dg/bypass-finals --dev
```

It requires PHP version 5.6 and supports PHP up to 7.2.


Usage
-----

Simply call this:

```php
DG\BypassFinals::enable();
```

You need to enable it before the classes you want to remove the final are loaded. So call it as soon as possible,
preferably right after `vendor/autoload.php` in loaded.
222 changes: 222 additions & 0 deletions src/BypassFinals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<?php

namespace DG;


/**
* Removes keyword final from source codes.
*/
class BypassFinals
{
const PROTOCOL = 'file';

/** @var resource|null */
public $context;

/** @var resource|null */
private $handle;

/** @var callable[] */
private static $mutators = [__CLASS__, 'removeFinals'];

This comment has been minimized.

Copy link
@vrana

vrana Sep 14, 2017

What is this good for?

This comment has been minimized.

Copy link
@dg

dg Sep 14, 2017

Author Owner

For nothing :)

This comment has been minimized.

Copy link
@vrana

vrana Sep 16, 2017

I thought that's some black magic.

This comment has been minimized.

Copy link
@dg

dg Sep 16, 2017

Author Owner

:-))



public static function enable()
{
stream_wrapper_unregister(self::PROTOCOL);
stream_wrapper_register(self::PROTOCOL, __CLASS__);
}


public function dir_closedir()
{
closedir($this->handle);
}


public function dir_opendir($path, $options)
{
$this->handle = $this->native('opendir', $path, $this->context);
return (bool) $this->handle;
}


public function dir_readdir()
{
return readdir($this->handle);
}


public function dir_rewinddir()
{
return rewinddir($this->handle);
}


public function mkdir($path, $mode, $options)
{
return $this->native('mkdir', $mode, false, $this->context);
}


public function rename($pathFrom, $pathTo)
{
return $this->native('rename', $pathFrom, $pathTo, $this->context);
}


public function rmdir($path, $options)
{
return $this->native('rmdir', $this->context);
}


public function stream_cast($castAs)
{
return $this->handle;
}


public function stream_close()
{
fclose($this->handle);
}


public function stream_eof()
{
return feof($this->handle);
}


public function stream_flush()
{
return fflush($this->handle);
}


public function stream_lock($operation)
{
return flock($this->handle, $operation);
}


public function stream_metadata($path, $option, $value)
{
switch ($option) {
case STREAM_META_TOUCH:
return $this->native('touch', $path, $value[0], $value[1]);
case STREAM_META_OWNER_NAME:
case STREAM_META_OWNER:
return $this->native('chown', $path, $value);
case STREAM_META_GROUP_NAME:
case STREAM_META_GROUP:
return $this->native('chgrp', $path, $value);
case STREAM_META_ACCESS:
return $this->native('chmod', $path, $value);
}
}


public function stream_open($path, $mode, $options, &$openedPath)
{
$usePath = (bool) ($options & STREAM_USE_PATH);
if (pathinfo($path, PATHINFO_EXTENSION) === 'php') {
$content = $this->native('file_get_contents', $path, $usePath, $this->context);
if ($content === false) {
return false;
}
$modified = self::removeFinals($content);
if ($modified !== $content) {
$this->handle = tmpfile();
$this->native('fwrite', $this->handle, $modified);
$this->native('fseek', $this->handle, 0);
return true;
}
}
$this->handle = $this->context
? $this->native('fopen', $path, $mode, $usePath, $this->context)
: $this->native('fopen', $path, $mode, $usePath);
return (bool) $this->handle;
}


public function stream_read($count)
{
return fread($this->handle, $count);
}


public function stream_seek($offset, $whence = SEEK_SET)
{
return fseek($this->handle, $offset, $whence);
}


public function stream_set_option($option, $arg1, $arg2)
{
}


public function stream_stat()
{
return fstat($this->handle);
}


public function stream_tell()
{
return ftell($this->handle);
}


public function stream_truncate($newSize)
{
return ftruncate($this->handle, $newSize);
}


public function stream_write($data)
{
return fwrite($this->handle, $data);
}


public function unlink($path)
{
return $this->native('unlink', $path);
}


public function url_stat($path, $flags)
{
return $this->native(
$flags & STREAM_URL_STAT_LINK ? 'lstat' : 'stat',
$path
);
}


private function native($func)
{
stream_wrapper_restore(self::PROTOCOL);
$res = call_user_func_array($func, array_slice(func_get_args(), 1));
stream_wrapper_unregister(self::PROTOCOL);
stream_wrapper_register(self::PROTOCOL, __CLASS__);
return $res;
}


public static function removeFinals($code)
{
if (strpos($code, 'final') !== false) {
$tokens = token_get_all($code);
$code = '';
foreach ($tokens as $token) {
$code .= is_array($token)
? ($token[0] === T_FINAL ? '' : $token[1])
: $token;
}
}
return $code;
}
}
Loading

0 comments on commit 6ea4df2

Please sign in to comment.