Skip to content

Commit 43bd27b

Browse files
committed
Initial commit
0 parents  commit 43bd27b

File tree

11 files changed

+186
-0
lines changed

11 files changed

+186
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
vendor
3+
composer.lock
4+
build

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
sudo: false
3+
php:
4+
- 7.1
5+
- 7.2
6+
7+
before_install:
8+
- composer self-update
9+
10+
install:
11+
- travis_retry composer install --no-interaction --prefer-source
12+
13+
script:
14+
- vendor/bin/phpstan analyze src/ test/ --level max
15+
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml
16+
17+
after_success:
18+
- travis_retry php vendor/bin/php-coveralls -v

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2018 Vsevolod Vietluzhskykh
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# TypeCasting - kind of explicit type casting in PHP
2+
3+
[![Build Status](https://travis-ci.com/Sevavietl/TypeCasting.svg?branch=master)](https://travis-ci.com/Sevavietl/TypeCasting)
4+
[![Coverage Status](https://coveralls.io/repos/github/Sevavietl/TypeCasting/badge.svg)](https://coveralls.io/github/Sevavietl/TypeCasting)
5+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6+
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat)](https://github.com/phpstan/phpstan)
7+
8+
Neither real [covariance](https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)) in method return type hints, nor [explicit type casting](https://www.baeldung.com/java-type-casting) are supported in PHP.
9+
10+
Sometimes it is useful to downcast object, so PhpStorm or PHPStan stop complaining about absent methods or mismatched types.

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "sevavietl/type-casting",
3+
"type": "library",
4+
"require": {
5+
"php": ">=7.1"
6+
},
7+
"require-dev": {
8+
"phpunit/phpunit": "^7.4",
9+
"phpstan/phpstan": "^0.10.5",
10+
"php-coveralls/php-coveralls": "^2.1"
11+
},
12+
"autoload": {
13+
"psr-4": {
14+
"TypeCasting\\": "src/"
15+
}
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"TypeCasting\\Test\\": "test/"
20+
}
21+
},
22+
"license": "MIT",
23+
"authors": [
24+
{
25+
"name": "sevavietl",
26+
"email": "sevavietl@gmail.com"
27+
}
28+
]
29+
}

phpunit.xml.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<filter>
12+
<whitelist processUncoveredFilesFromWhitelist="true">
13+
<directory suffix=".php">./src</directory>
14+
</whitelist>
15+
</filter>
16+
<testsuites>
17+
<testsuite name="unit">
18+
<directory suffix="Test.php">test/</directory>
19+
</testsuite>
20+
</testsuites>
21+
</phpunit>

src/TypeCasting.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace TypeCasting;
4+
5+
trait TypeCasting
6+
{
7+
public static function cast($object): self
8+
{
9+
return $object;
10+
}
11+
}

test/Animal.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace TypeCasting\Test;
4+
5+
class Animal
6+
{
7+
}

test/Cat.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace TypeCasting\Test;
4+
5+
use TypeCasting\TypeCasting;
6+
7+
final class Cat extends Animal
8+
{
9+
use TypeCasting;
10+
11+
public function meow(): string
12+
{
13+
return 'meow';
14+
}
15+
}

test/Dog.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace TypeCasting\Test;
4+
5+
use TypeCasting\TypeCasting;
6+
7+
class Dog extends Animal
8+
{
9+
use TypeCasting;
10+
11+
public function bark(): string
12+
{
13+
return 'bark';
14+
}
15+
}

0 commit comments

Comments
 (0)